CHAPTER 2
Single-Table Queries
85
SELECT
DATEADD(
month,
DATEDIFF(month, '20010101', CURRENT_TIMESTAMP), '20010101');
Similarly, by using a year part and an anchor that is the first day of a year, you get back the first
day of the current year.
If you want the last day of the month or year, simply use an anchor that is the last day of a month
or year. For example, the following expression returns the last day of the current month.
SELECT
DATEADD(
month,
DATEDIFF(month, '19991231', CURRENT_TIMESTAMP), '19991231');
Note that in SQL Server 2012 there’s a simpler way to get the last day of the month: by using a new
function called EOMONTH. I’ll describe it shortly.
The
DATEPART
Function
The DATEPART function returns an integer representing a requested part of a date and time value.
Syntax
DATEPART(
Do'stlaringiz bilan baham: |