Sql Date Manipulation

DATEPART (datepart, date) returns the desired part of the date specified by datepart parameter: year,
quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond.

Example: DATEPART(month, GETDATE()) returns current month number

DAY(date), MONTH(date), YEAR(date) return respectively the number of the day, month or year of the passed date.

To add or subtract date parts from a given date use this function:
DATEADD (datepart , number, date)
where datepart is one of the following parameter (datepart): year,
quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond.

Example: DATEADD(day, -7, GETDATE()) return the date week ago.

DATEDIFF (datepart, startdate, enddate) gives you the difference between two dates in desired date part (see DATEPART date parts above). Startdate should be date before end date. Otherwise a negative number will be returned.

Example: DATEADD(day, DATEADD(day, -7, GETDATE()), GETDATE()) return 7 days.

No comments:

Post a Comment