1 ---ÉϸöÔÂÔ³õµÚÒ»Ìì
2 select CONVERT(varchar(12) , DATEADD(mm,DATEDIFF(mm,0,dateadd(mm,-1,getdate())),0), 112 )
3
4 ---ÉϸöÔÂÔÂÄ©×îºóÒ»Ìì
5 select CONVERT(varchar(12),dateadd(ms,-3,DATEADD(mm,DATEDIFF(m,0,getdate()),0)), 112 )
6
7 --1. ÏÔʾ±¾ÔµÚÒ»Ìì
8
9 SELECT DATEADD(mm,DATEDIFF(mm,0,getdate()),0)
10
11 select convert(datetime,convert(varchar(8),getdate(),120)+'01',120)
12
13 --2. ÏÔʾ±¾ÔÂ×îºóÒ»Ìì
14
15 select dateadd(day,-1,convert(datetime,convert(varchar(8),dateadd(month,1,getdate()),120)+'01',120))
16
17 SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(m,0,getdate())+1,0))
18
19 --3. ÉϸöÔµÄ×îºóÒ»Ìì
20
21 SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(mm,0,getdate()),0))
22
23 --4. ±¾ÔµĵÚÒ»¸öÐÇÆÚÒ»
24 select DATEADD(wk,DATEDIFF(wk,0, dateadd(dd,6-datepart(day,getdate()),getdate())),0)
25
26 --5. ±¾ÄêµÄµÚÒ»Ìì
27
28 SELECT DATEADD(yy,DATEDIFF(yy,0,getdate()),0)
29
30 --6. ±¾ÄêµÄ×îºóÒ»Ìì
31
32 SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))
33
34 --7. È¥ÄêµÄ×îºóÒ»Ìì
35
36 SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate()),0))
37
38 --8. ±¾¼¾¶ÈµÄµÚÒ»Ìì
39
40 SELECT DATEADD(qq,DATEDIFF(qq,0,getdate()),0)
41
42 --9. ±¾ÖܵÄÐÇÆÚÒ»
43
44 SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0)
45
46 --10. ²éѯ±¾ÔµļǼ
47
48 select * from tableName where DATEPART(mm, theDate)=DATEPART(mm, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
49
50 --11. ²éѯ±¾ÖܵļǼ
51
52 select * from tableName where DATEPART(wk, theDate) = DATEPART(wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
53
54 --12 ²éѯ±¾¼¾µÄ¼Ç¼ ×¢£ºÆäÖÐ:GETDATE() ÊÇ»ñµÃϵͳʱ¼äµÄº¯Êý¡£
55
56 select * from tableName where DATEPART(qq, theDate) = DATEPART(qq, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
57
58 --13. »ñÈ¡µ±ÔÂ×ÜÌìÊý:
59
60 select DATEDIFF(dd,getdate(),DATEADD(mm, 1, getd
¶øÕâһƪÖУ¬ÎÒÃǾÍΧÈÆSQLÓÅ»¯À´¿ªÊ¼Õâ´Î½²½â£¬ÎªÊ²Ã´µÚÒ»½²ÒªËµSQLÓÅ»¯£¿ÒòΪÎÒÈÏΪÕâÊdzÌÐòÔ±µÄ»ù±¾¹¦£¬¶øÇÒÒ²ÊÇÎÒÃDZØÐëҪȥÕÆÎյģ¬ËäÈ»ÄãдµÄ SQLÓï¾äÄÜÍê³ÉÏàÓ¦µÄ¹¦ÄÜ£¬µ«ÊÇÄãÊÇ·ñ¿¼ÂǹýÕâЩÓï¾äÅöµ½º£Á¿Êý¾Ý»òÕß±©Á¦·ÃÎÊʱ»á²»»á´øÀ´Ð§ÂʵĴó·ù¶ÈµÄ¼õÂý£¿Ò²ÐíºÜ¶à³ÌÐòÔ±ºÍÎÒÒ»ÑùÔÚÅöµ½ÏµÍ³ÏìÓ¦Ê ......
Ò»¸öÀý×Ó£º´Ótszl±íÖвé³öÊý¾Ý£¬¸ù¾Ýcs×ֶεÄÖµ¾ö¶¨ÍùBOOK_SERIAL±íÖвåÈ뼸ÐÐÊý¾Ý¡£
declare @num int
declare @id varchar(60)
declare @classbm varchar(60)
set @classBm='101'
declare id cursor for select id,cs from [tszl]
open id
fetch next from id into @id,@num
while @@fetch_status!=-1
&nb ......