关于日期函数的一些整理

转自:http://blog.csdn.net/happyflystone/article/details/2466055 :)

 Declare   @d   DATETIME
set   @d   =   ' 2008-05-21 '
select   @d   as  当前时间
-- 1、当前时间周的星期一
SELECT    DATEADD (wk,   DATEDIFF (wk, 0 , @d ),   0 )   as  当前时间周的星期一

-- 2、当前时间周的星期日
SELECT    DATEADD (wk,   DATEDIFF (wk, 0 , @d ),   0 ) + 6  当前时间周的星期日

-- 3、当前时间月的星期一
SELECT    DATEADD (mm,   DATEDIFF (mm, 0 , @d ),   0 +   9   -   datepart (dw,  DATEADD (mm,   DATEDIFF (mm, 0 , @d ),   0 )) 当前时间月的星期一
-- 4、当前时间月的星期日
SELECT    DATEADD (mm,   DATEDIFF (mm, 0 , @d ),   0 +   8   -   datepart (dw,  DATEADD (mm,   DATEDIFF (mm, 0 , @d ),   0 )) 当前时间月的星期日
-- 5、当前时间月的总天数

SELECT  当前时间月的总天数1 = DAY ( dateadd (ms, - 100 , DATEADD (mm,  DATEDIFF (m, 0 , @d ) + 1 , 0 )))
SELECT  当前时间月的总天数2 = 32 - DAY ( @d - DAY ( @d ) + 32 -- 此条收录于网上




/*   
                                                    
当前时间                                                   
------------------------------------------------------ 
2008-04-21 00:00:00.000

当前时间周的星期一                                              
------------------------------------------------------ 
2008-04-21 00:00:00.000

当前时间周的星期日                                              
------------------------------------------------------ 
2008-04-27 00:00:00.000

当前时间月的星期一                                              
------------------------------------------------------ 
2008-04-07 00:00:00.000

当前时间月的星期日                                              
------------------------------------------------------ 
2008-04-06 00:00:00.000

当前时间月的总天数1  
----------- 
30

当前时间月的总天数2  
----------- 
30


*/


-------------------------------------------------------------------------
-- 以下收录于网上

DECLARE   @dt   datetime
SET   @dt = GETDATE ()

DECLARE   @number   int
SET   @number = 3

-- 1.指定日期该年的第一天或最后一天
--
A. 年的第一天
SELECT   CONVERT ( char ( 5 ), @dt , 120 ) + ' 1-1 '

-- B. 年的最后一天
SELECT   CONVERT ( char ( 5 ), @dt , 120 ) + ' 12-31 '


-- 2.指定日期所在季度的第一天或最后一天
--
A. 季度的第一天
SELECT   CONVERT ( datetime ,
    
CONVERT ( char ( 8 ),
        
DATEADD ( Month ,
            
DATEPART (Quarter, @dt ) * 3 - Month ( @dt ) - 2 ,
            
@dt ),
        
120 ) + ' 1 ' )

-- B. 季度的最后一天(CASE判断法)
SELECT   CONVERT ( datetime ,
    
CONVERT ( char ( 8 ),
        
DATEADD ( Month ,
            
DATEPART (Quarter, @dt ) * 3 - Month ( @dt ),
            
@dt ),
        
120 )
    
+ CASE   WHEN   DATEPART (Quarter, @dt in ( 1 , 4 )
        
THEN   ' 31 ' ELSE   ' 30 '   END )

-- C. 季度的最后一天(直接推算法)
SELECT   DATEADD ( Day , - 1 ,
    
CONVERT ( char ( 8 ),
        
DATEADD ( Month ,
            
1 + DATEPART (Quarter, @dt ) * 3 - Month ( @dt ),
            
@dt ),
        
120 ) + ' 1 ' )


-- 3.指定日期所在月份的第一天或最后一天
--
A. 月的第一天
SELECT   CONVERT ( datetime , CONVERT ( char ( 8 ), @dt , 120 ) + ' 1 ' )

-- B. 月的最后一天
SELECT   DATEADD ( Day , - 1 , CONVERT ( char ( 8 ), DATEADD ( Month , 1 , @dt ), 120 ) + ' 1 ' )

-- C. 月的最后一天(容易使用的错误方法)
SELECT   DATEADD ( Month , 1 , DATEADD ( Day , - DAY ( @dt ), @dt ))


-- 4.指定日期所在周的任意一天
SELECT   DATEADD ( Day , @number - DATEPART (Weekday, @dt ), @dt )


-- 5.指定日期所在周的任意星期几
--
A.  星期天做为一周的第1天
SELECT   DATEADD ( Day , @number - ( DATEPART (Weekday, @dt ) + @@DATEFIRST - 1 ) % 7 , @dt )

-- B.  星期一做为一周的第1天
SELECT   DATEADD ( Day , @number - ( DATEPART (Weekday, @dt ) + @@DATEFIRST - 2 ) % 7 - 1 , @dt )

/*
-------- 
2008-1-1

           
---------- 
2008-12-31

                                                       
------------------------------------------------------ 
2008-04-01 00:00:00.000

                                                       
------------------------------------------------------ 
2008-06-30 00:00:00.000

                                                       
------------------------------------------------------ 
2008-06-30 00:00:00.000

                                                       
------------------------------------------------------ 
2008-05-01 00:00:00.000

                                                       
------------------------------------------------------ 
2008-05-31 00:00:00.000

                                                       
------------------------------------------------------ 
2008-05-30 14:32:08.963

                                                       
------------------------------------------------------ 
2008-05-20 14:32:08.963

                                                       
------------------------------------------------------ 
2008-05-21 14:32:08.963

                                                       
------------------------------------------------------ 
2008-05-21 14:32:08.963

*/

你可能感兴趣的:(算法)