---上個月月初第一天
select CONVERT(varchar(12) , DATEADD(mm,DATEDIFF(mm,0,dateadd(mm,-1,getdate())),0), 112 )
---上個月月末最後一天
select CONVERT(varchar(12),dateadd(ms,-3,DATEADD(mm,DATEDIFF(m,0,getdate()),0)), 112 )
1. 顯示本月第一天
SELECT DATEADD(mm,DATEDIFF(mm,0,getdate()),0)
select convert(datetime,convert(varchar(8),getdate(),
120)+'01',120)
2. 顯示本月最後一天
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. 上個月的最後一天
SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(mm,0,getdate()),0))
4. 本月的第一個星期一i
select DATEADD(wk,DATEDIFF(wk,0, dateadd(dd,6-datepart(
day,getdate()),getdate())),0)
5. 本年的第一天
SELECT DATEADD(yy,DATEDIFF(yy,0,getdate()),0)
6. 本年的最後一天
SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))
7. 去年的最後一天
SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate()),0))
8. 本季度的第一天
SELECT DATEADD(qq,DATEDIFF(qq,0,getdate()),0)
9. 本周的星期一
SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0)
10. 查詢本月的記錄
select * from tableName where DATEPART(mm, theDate)
=DATEPART(mm, GETDATE()) and DATEPART(yy, theDate)
= DATEPART(yy, GETDATE())
11. 查詢本周的記錄
select * from tableName where DATEPART(wk, theDate) = DATEPART
(wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
12 查詢本季的記錄 註:其中:GETDATE() 是獲得系統時間的函數。
select * from tableName where DATEPART(qq, theDate) = DATEPART
(qq, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
13. 擷取當月總天數:
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. 擷取當前為星期幾
DATENAME(weekday, getdate())
-------------------------------------------------------------------------------------------
1. select CONVERT(varchar(12) , getdate(), 101 )
12/28/2007
2. select CONVERT(varchar(12) , getdate(), 102 )
2007.12.28
3. select CONVERT(varchar(12) , getdate(), 103 )
28/12/2007
4. select CONVERT(varchar(12) , getdate(), 104 )
28.12.2007
5. select CONVERT(varchar(12) , getdate(), 105 )
28-12-2007
6. select CONVERT(varchar(12) , getdate(), 106 )
28 12 2007
7. select CONVERT(varchar(12) , getdate(), 107 )
12 28, 2007
8. select CONVERT(varchar(12) , getdate(), 108 )
10:06:33
9. select CONVERT(varchar(12) , getdate(), 109 )
12 28 2007 1
10.select CONVERT(varchar(12) , getdate(), 110 )
12-28-2007
11.select CONVERT(varchar(12) , getdate(), 111 )
2007/12/28
12.select CONVERT(varchar(12) , getdate(), 112 )
20071228
13.select CONVERT(varchar(12) , getdate(), 113 )
28 12 2007 1
14.select CONVERT(varchar(12) , getdate(), 114 )
10:07:36:623
15.select CONVERT(varchar, getdate(), 120 )
2007-12-28 10:08:56
select CONVERT(varchar(10), getdate(), 120 )
2007-12-28
16.select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
20071228101012
1.顯示本月第一天
SELECT DATEADD(mm,DATEDIFF(mm,0,getdate()),0)
select convert(datetime,convert(varchar(8),getdate(),120)+'01',120)
2.顯示本月最後一天
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.上個月的最後一天
SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(mm,0,getdate()),0))
4.本月的第一個星期一
select DATEADD(wk,DATEDIFF(wk,0, dateadd(dd,6-datepart(day,getdate()),getdate())),0)
5.本年的第一天
SELECT DATEADD(yy,DATEDIFF(yy,0,getdate()),0)
6.本年的最後一天
SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))
7.去年的最後一天
SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate()),0))
8.本季度的第一天
SELECT DATEADD(qq,DATEDIFF(qq,0,getdate()),0)
9.本周的星期一
SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0)
10.查詢本月的記錄
select * from tableName where DATEPART(mm, theDate) = DATEPART(mm, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
11.查詢本周的記錄
select * from tableName where DATEPART(wk, theDate) = DATEPART(wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
12.查詢本季的記錄
select * from tableName where DATEPART(qq, theDate) = DATEPART(qq, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
其中:GETDATE()是獲得系統時間的函數。
13.擷取當月總天數:
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.擷取當前為星期幾
DATENAME(weekday, getdate())