Date/Time Functions

Date/Time Functions

Function Name

.NET Method

Example

GETDATE

System.DateTime.Now

SELECT getdate()

GETUTCDATE

System.DateTime.UtcNow

SELECT getutcdate()

YEAR

System.DateTime.Year

SELECT year(getdate()), year('7/16/2001 6:28:55 PM'), year(NULL)

MONTH

System.DateTime.Month

SELECT month(getdate()), month('7/16/2001 6:28:55 PM'), month(NULL)

DAY

System.DateTime.Day

SELECT day(getdate()), day('7/16/2001 6:28:55 PM'), day(NULL)

HOUR

System.DateTime.Hour

SELECT hour(getdate()), hour('7/16/2001 6:28:55 PM'), hour(NULL)

MINUTE

System.DateTime.Minute

SELECT minute(getdate()), minute('7/16/2001 6:28:55 PM'), minute(NULL)

SECOND

System.DateTime.Second

SELECT second(getdate()), second('7/16/2001 6:28:55 PM'), second(NULL)

MILLISECOND

System.DateTime.Millisecond

SELECT millisecond('7/16/2001 6:28:55.999 PM'), millisecond(NULL)

DAYOFWEEK

System.DateTime.DayOfWeek

SELECT dayofweek('7/16/2005 6:28:55.999 PM'), dayofweek(NULL)

DAYOFYEAR

System.DateTime.DayOfYear

SELECT dayofyear('7/16/2005 6:28:55.999 PM'), dayofyear(NULL)

TIMEOFDAY

System.DateTime.TimeOfDay

SELECT TimeOfDay('7/16/2005 6:28:55.999 PM'), TimeOfDay(NULL)

Remarks:

Given the following .NET code

DateTime.Now.Subtract(certainDate).TotalDays;

the equivalent SQL function calls would be

SELECT DAY(SUBTRACT(GETDATE(), certainDate)) FROM table