SQL date calculations based on how many days of the month (turn)

Original link: https://blog.csdn.net/dobear_0922/article/details/2393235

- 1. Remove the test table
the IF the OBJECT_ID (N'Test ', N'u') the NOT NULL the IS
    the DROP TABLE the Test

--2.建立测试表,并填充测试数据
SELECT * INTO Test 
FROM
(
    SELECT CONVERT(DATETIME, '2008-1-1', 120) AS dt UNION ALL SELECT '2008-1-15' UNION ALL SELECT '2008-1-31'
    UNION ALL SELECT '2008-2-1' UNION ALL SELECT '2008-2-15' UNION ALL SELECT '2008-2-29'
    UNION ALL SELECT '2007-2-1' UNION ALL SELECT '2007-2-15' UNION ALL SELECT '2007-2-28'
    UNION ALL SELECT '2007-4-1' UNION ALL SELECT '2007-4-15' UNION ALL SELECT '2007-4-30'
) T

. --3 calculated according to the date of the month, how many days
the SELECT *
    - month = 32 days - [(finally +32 days one day last month) date value]
    --eg '2008-1': Day-32 ( '2008 -2-1 ')' 2008-2 ': day-32 (' 2008-3-3 ')
    , 32-dAY = DS1 (dAY-dt (dt) +32)
    
    - = number of days of the month of the date of the last day of the month value
    --eg '2008-1': Day ( '2008-1-31') '2008-2': Day ( '2008-2-29')
    , DAY = DS2 (the DATEADD (mm,. 1, dt) - DAY (the DATEADD (mm,. 1, dt)))    
    , DAY = DS3 (the DATEADD (mm, MONTH (dt), dt-DATEPART (Dy, dt)))    
    , DS4 = DAY (the DATEADD (D, -1, the CONVERT ( VARCHAR (8), DATEADD (m , 1, dt), 120) + '01'))

    - the difference = number of days of the month day of the month to the next month 1st One
    --eg DATEDIFF (D, '2008-1-1', '2008-2-1')
    , the DATEDIFF DS5 = (D, the DATEADD ( dd,. 1-DAY (dt), dt), the DATEADD (mm,. 1, the DATEADD (dd,. 1-DAY (dt), dt)))
    , DS6 = the DATEDIFF (D, the DATEADD (m, the DATEDIFF (m, 0, dt), 0), the DATEADD (m, DATEDIFF (m, 0, dt) + 1'd, 0))
    , the DATEDIFF DS7 = (D, the CONVERT (VARCHAR (. 8), dt, 120) + '01', the CONVERT (VARCHAR (. 8), the DATEADD (m,. 1, dt), 120) + '01')        
from the Test

/*
dt                      ds1         ds2         ds3         ds4         ds5         ds6         ds7
----------------------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
2008-01-01 00:00:00.000 31          31          31          31          31          31          31
2008-01-15 00:00:00.000 31          31          31          31          31          31          31
2008-01-31 00:00:00.000 31          31          31          31          31          31          31
2008-02-01 00:00:00.000 29          29          29          29          29          29          29
2008-02-15 00:00:00.000 29          29          29          29          29          29          29
2008-02-29 00:00:00.000 29          29          29          29          29          29          29
2007-02-01 00:00:00.000 28          28          28          28          28          28          28
2007-02-15 00:00:00.000 28          28          28          28          28          28          28
2007-02-28 00:00:00.000 28          28          28          28          28          28          28
2007-04-01 00:00:00.000 30          30          30          30          30          30          30
2007-04-15 00:00:00.000 30          30          30          30          30          30          30
2007-04-30 00:00:00.000 30          30          30          30          30          30          30

(12 row(s) affected)
*/

. --4 delete the test table
DROP TABLE the Test
----------------
Original link: https: //blog.csdn.net/dobear_0922/article/details/2393235

Guess you like

Origin www.cnblogs.com/buduzhiren/p/11459521.html