SQL Server Date functions DateAdd() DateDiff(), DatePart(), GetDate()

Note: This article is intended for beginners, who want to gain more knowledge and understanding on SQL Server Date Function..

Here I am explaining about some of the commonly used Date Functions. in my previous one article i have already explained about DATENAME() function which i am not going to explain again. You can find that article here DateName() in SQL Server.

So let us continue with my point of discussion here.

DateAdd() – Returns a new datetime value based on adding an interval to the specified date.
Where first parameter specifies on which part of the date to return a new value: yy(yyyy) for Year, mm(m) for Month, dd(d) for Day etc.
For example: select dateadd(d,2,getdate()) – adds 2 days to current date and returns new date.

DateDiff() – Returns the number of date and time boundaries crossed between two specified dates.
Where DateDiff( <interval>, <start date>, <end date>): <Start Date> is subtracted from <End Date>. If <Start Date> is later than <End Date>, a negative value is returned.

DatePart() – Returns an integer representing the specified datepart of the specified date.
Where DatePart( <interval>, <Date>)

Where the datepart interval parameter is one of the following:
year,
quarter,
month,
dayofyear,
day,
week,
hour,
minute,
second or
millisecond.

The number datepart interval parameter is an integer value for the number of dateparts to be added to or subtracted from the date parameter. Now depending on your needs, your application might use this function to perform mathematical calculations on a given date.

The DATEDIFF function is used to calculate the number of date and time boundaries crossed between two different dates. This function returns an integer value.

GetDate() – Returns the current system date and time in the Microsoft® SQL Server™ standard internal format for datetime values. SQL Server provides the GetDate() function to get UTC time. The GetDate() function returns a datetime value that represents the current UTC time.

Summary

I have explained the general/basic usage of some of the SQL Server functions GetDate(), DateDiff(), DateAdd(), DateName() etc.

I hope you have got basic understanding about the datetime functions in sql server.

That’s all for now. Stay tuned.. i will post more articles soon.

Happy learning and coding…