Thursday, 28 September 2006

SQL Server Date and Time Functions

Date Time functions can be found in the online book. I list them here for quick reference.


DATEADD(datepart, integer, datetoadd) Returns a new datetime value based on the parameters.
DATEDIFF(datepart, date1, date2) Returns number of time units between two dates
DATENAME(datepart, datetoadd) Returns the full name of the date part. eg

select DATENAME(month, getdate()) as month_name
month_name
----------------------
July
DATEPART(datepart, date) Returns part of a date
DAY(date), MONTH(date), YEAR(date) Returns specific part of a date.

Date Part abbreviations


year yy, yyyy
quarter q, qq
month m, mm
day of year dy, y
day of month day
week ww, wk
hour hh,
minute mi, n
second ss, s
millisecond ms

No comments: