2552-10-02

select วันสุดท้ายของเดือน, day in month

----Last Day of Previous Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
LastDay_PreviousMonth
----Last Day of Current Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
LastDay_CurrentMonth
----Last Day of Next Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0))
LastDay_NextMonth



-- select dayofyear 
-- How many day of year ,u can change "getdate()" to other datetime 
select DATEDIFF(DAY
, DATEADD(D,-DAY(getdate())+1, DATEADD(MM,-MONTH(getdate())+1,getdate()))
,DATEADD(YEAR,1, DATEADD(D,-DAY(getdate())+1, DATEADD(MM,-MONTH(getdate())+1,getdate())))
)

1 ความคิดเห็น:

  1. -- select dayofyear
    -- How many day of year ,u can change "getdate()" to other datetime
    select DATEDIFF(DAY
    , DATEADD(D,-DAY(getdate())+1, DATEADD(MM,-MONTH(getdate())+1,getdate()))
    ,DATEADD(YEAR,1, DATEADD(D,-DAY(getdate())+1, DATEADD(MM,-MONTH(getdate())+1,getdate())))
    )

    ตอบลบ