Types of Datetime Functions
1. Getdate
2. Day
3. Month
4. Year
5.Datename
6.Dateadd
7.Datediff
8.Datepart
Getdate():Getdate function return current time and date .
Syntax:
- select Getdate();
Day(): This function is return day value from the given date
Syntax:
- select DAY('2018/10/31')
Month:This function is return Month value from the given date
Syntax:- select Month('2018/10/31')
Year():this function is return year value from the given date
- select year('2018/10/31')
Datename():It return the name of the day and month of give date expression.
- select DATENAME(DW,'2018/10/31')
- select DATENAME(MM,'2018/10/31')
- select DATEADD('Day/month/year','No. of number want to add','Date')
- select DATEADD(dd,5,getdate())
- select DATEADD(MM,1,getdate())
- select DATEADD(YYYY,1,getdate())

Datediff():Datediff is use to find difference between dates.it will return the differences between starting and ending date expressions
- select datediff('Day/month/year','startDate','endDate')
- select DATEDIFF(dd,'2017/2/3','2018/3/5') as TotalDays
- select DATEDIFF(MM,'2017/2/3','2018/3/5') as TotalMonths
- select DATEDIFF(YYYY,'2017/2/3','2018/3/5') as Totalyear
- select DATEDIFF(WK,'2017/2/3','2018/3/5') as TotalWeeks
- select DATEDIFF(HH,'2017/2/3','2018/3/5') As TotalHours
- select DATEDIFF(MI,'2017/2/3','2018/3/5') As TotalMin

create a Table
- create table Userdate(ID int identity,birthdate date)
Add some value in the Table
- insert into Userdate values('1994/12/30')
- insert into Userdate values('1995/7/4')
- insert into Userdate values('2000/9/1')
- insert into Userdate values('1999/12/30')
Now find the difference Use this query
- select a1.birthdate ,datediff(day,a1.birthdate,a2.birthdate)as DobDiff from Userdate a1 inner join Userdate a2 on a2.id=a1.id+1

Datepart():It return day,month and year values from the given date expression
- select DATEPART(day,getdate()) as Days
- select DATEPART(MM,getdate()) as Month
- select DATEPART(YYYY,getdate()) as Year
- select DATEPART(WK,getdate()) as Weaks
- select DATEPART(HH,getdate()) as Hours

Summary:In this article we Learned datetime functions in SQL Server.Datetime functions are Pre Defined function of SQL.

Rahul PrajapatPosted Jul 5, 2015, 10:29 PM
NIce information pankaj sir, thanks
Neeraj KumarPosted Jul 3, 2015, 3:02 PM
Good.
Santhakumar MunuswamyPosted Jul 3, 2015, 2:37 PM
Good work
Vipul MalhotraPosted Jul 3, 2015, 3:43 AM
Nice
Sibeesh VenuPosted Jul 3, 2015, 1:37 AM
Good one.
Debendra DashPosted Jul 3, 2015, 1:26 AM
Good one Pankaj...
Rajeesh MenothPosted Jul 3, 2015, 1:26 AM
Good One
Upendra Pratap ShahiPosted Jul 3, 2015, 12:36 AM
very nice Pankaj Kumar Choudhary
Yashwant VishwakarmaPosted Jul 3, 2015, 12:20 AM
Truly awesome !! :)
Manoj KulkarniPosted Jul 2, 2015, 11:17 PM
Good One
RakeshPosted Jul 2, 2015, 11:04 PM
Good