python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 7, Interview Questions Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
How to add Days to DateTime in Sql Server?

How To Add Days, Months, Quarters and Years To DateTime in SQL Server?

How to add Days to DateTime in Sql Server?
How to add Weeks to DateTime in Sql Server?
How to add Months to DateTime in Sql Server?
How to add Quarters to DateTime in Sql Server?
How to add Years to DateTime in Sql Server?

1: How to add Days to DateTime in Sql Server?

Definition:-

The DATEADD() function adds or subtracts a specified time interval from a date.

Syntax – 
DATEADD(DATEPART, NUMBER, DATETIME)

SELECT DATEADD(DAY, -(2), GETDATE()) AS DATEADD_DAYS

Result - 
DATEADD_DAYS
-------------------------
2017-04-18 16:37:47.833

The date part will be,
a)      Year          
b)      Quarter    
c)      Month      
d)      Dayofyear
e)      Day           
f)       Week        
g)      Weekday  
h)      Hour         
i)        Minute     
j)        Second      
k)      millisecond           
l)        Microsecond
m)   Manosecond etc.

Example as,
--INPUTS PARAMS
DECLARE @From_Date DATETIME = '2017-01-31'
DECLARE @UID BIGINT =19975

-- SELECT QUERY TO BIND BILING CHART.
SELECT PERIOD_DATE, 
	PERIOD, 
	SUM(COST) AS COST 
FROM UVW_BIL_BILL_SUMM
WHERE UID= @UID
	AND PERIOD_DATE >= DATEADD(DAY, -(2), @From_Date)
GROUP BY PERIOD_DATE, PERIOD --GROUP BY COULUMNS
ORDER BY PERIOD_DATE -- ORDER BY COULUMNS

2: How to add Weeks to DateTime in Sql Server?

Syntax - 

SELECT DATEADD(WEEK, -(2), GETDATE()) AS DATEADD_WEEK

Example as,
--INPUTS PARAMS
DECLARE @From_Date DATETIME = '2017-01-31'
DECLARE @UID BIGINT =19975

-- SELECT QUERY TO BIND BILING CHART.
SELECT PERIOD_DATE, 
	PERIOD, 
	SUM(COST) AS COST 
FROM UVW_BIL_BILL_SUMM
WHERE UID= @UID
	AND PERIOD_DATE >= DATEADD(WEEK, -(2), @From_Date)
GROUP BY PERIOD_DATE, PERIOD --GROUP BY COULUMNS
ORDER BY PERIOD_DATE -- ORDER BY COULUMNS

3: How to add Months to DateTime in Sql Server?

Syntax - 

SELECT DATEADD(MONTH, -(2), GETDATE()) AS DATEADD_MONTH

Example as,
--INPUTS PARAMS
DECLARE @From_Date DATETIME = '2017-01-31'
DECLARE @UID BIGINT =19975

-- SELECT QUERY TO BIND BILING CHART.
SELECT PERIOD_DATE, 
	PERIOD, 
	SUM(COST) AS COST 
FROM UVW_BIL_BILL_SUMM
WHERE UID= @UID
	AND PERIOD_DATE >= DATEADD(MONTH, -(2), @From_Date)
GROUP BY PERIOD_DATE, PERIOD --GROUP BY COULUMNS
ORDER BY PERIOD_DATE -- ORDER BY COULUMNS

4: How to add Quarters to DateTime in Sql Server?
Syntax - 

SELECT DATEADD(QUARTER, -(2), GETDATE()) AS DATEADD_QUARTER

Example as,
--INPUTS PARAMS
DECLARE @From_Date DATETIME = '2017-01-31'
DECLARE @UID BIGINT =19975

-- SELECT QUERY TO BIND BILING CHART.
SELECT PERIOD_DATE, 
	PERIOD, 
	SUM(COST) AS COST 
FROM UVW_BIL_BILL_SUMM
WHERE UID= @UID
	AND PERIOD_DATE >= DATEADD(QUARTER, -(2), @From_Date)
GROUP BY PERIOD_DATE, PERIOD --GROUP BY COULUMNS
ORDER BY PERIOD_DATE -- ORDER BY COULUMNS

5: How to add Years to DateTime in Sql Server?

Syntax - 

SELECT DATEADD(YEAR, -(2), GETDATE()) AS DATEADD_YEAR

Example as,

--INPUTS PARAMS
DECLARE @From_Date DATETIME = '2017-01-31'
DECLARE @UID BIGINT =19975

-- SELECT QUERY TO BIND BILING CHART.
SELECT PERIOD_DATE, 
	PERIOD, 
	SUM(COST) AS COST 
FROM UVW_BIL_BILL_SUMM
WHERE UID= @UID
	AND PERIOD_DATE >= DATEADD(YEAR, -(2), @From_Date)
GROUP BY PERIOD_DATE, PERIOD --GROUP BY COULUMNS
ORDER BY PERIOD_DATE -- ORDER BY COULUMNS

How to add Days, Weeks, Months, Quarters and Years to DateTime in Sql Server?



I hope you are enjoying with this post! Please share with you friends. Thank you!!
ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions