Is any date function like last day, first day (SQL server)?

  • Thanks Zd.

  • --FirstDayofMonth

    select datename(dw,CONVERT(DATETIME, CONVERT(VARCHAR,datepart(M,getdate())) + '/1/' + CONVERT(VARCHAR, datepart(YYYY,getdate()))))

    --LastDayOfMonth

    select datename(dw,dateadd(dd,-1,dateadd(mm,1,convert(varchar(7),getdate(),120)+'-01')))

    Thanks,

    Ganesh

  • Great,

    and which day is last of mont (31.1., 28.2., .......), but no datename.

    thanks

  • To get the last day of a month one can use something like this

    SELECT

    DATEADD(DAY, -DAY(GETDATE()), CAST(CONVERT(CHAR(8), GETDATE(), 112) AS DATETIME))

    AS Monatsultimo

    However, using integer operations should be faster:

    SELECT DATEADD(d,DATEDIFF(d,DAY(GETDATE()),GETDATE()),0) AS Monatsultimo

    A pretty cool script comes from SQL Server MVP Steve Kass:

    declare @31st datetime

    set @31st = '19341031' -- any 31st

    declare @now datetime

    set @now = getdate()

    select dateadd(month,datediff(month,@31st,@now),@31st)

    Just make sure, your starting date is any valid 31st, no matter whether in the past or future.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply