1. display the first day of the month
SELECT DATEADD(mm,DATEDIFF(mm,0,getdate()),0)
select convert(datetime,convert(varchar(8),getdate(),
120)+'01',120)
|
2. display the last day of the month
select dateadd(day,-1,convert(datetime,convert
(varchar(8),dateadd(month,1,getdate()),120)+'01',120))
SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(m,0,getdate())+1,0))
|
3. Last day of last month
SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(mm,0,getdate()),0))
|
4. the first Monday of this month
select DATEADD(wk,DATEDIFF(wk,0, dateadd(dd,6-datepart(
day,getdate()),getdate())),0)
|
5. The first day of this year
SELECT DATEADD(yy,DATEDIFF(yy,0,getdate()),0)
|
6. The last day of the year
SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0)
|
7. Last day of last year
SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate()),0))
|
8. The first day of this quarter
SELECT DATEADD(qq,DATEDIFF(qq,0,getdate()),0)
|
9. Monday of the week
SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0)
|
Last Monday
SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0)-7
SELECT DATEADD(wk,DATEDIFF(wk,0,getdate())-1,0)
10. query records of this month
select * from tableName where DATEPART(mm, theDate)
=DATEPART(mm, GETDATE()) and DATEPART(yy, theDate)
= DATEPART(yy, GETDATE())
|
11. query the records of this week
select * from tableName where DATEPART(wk, theDate) = DATEPART
(wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
|
12. query this season's recordsNote: getdate () is a function used to obtain the system time.
select * from tableName where DATEPART(qq, theDate) = DATEPART
(qq, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
|
13. Get the total number of days of the month:
select DATEDIFF(dd,getdate(),DATEADD
(mm, 1, getdate()))
select datediff(day,
dateadd(mm, datediff(mm,'',getdate()), ''),
dateadd(mm, datediff(mm,'',getdate()), '1900-02-01'))
|
14. Obtain the current day of the week
DATENAME(weekday, getdate())