Datetime conversion

  • Hello,

    I am trying to replace set of hard-coded datetime with datetimes dynamically captured; but the result I am getting are not same, can somebody suggest what I am missing.

    Tnx

    Declare @startdate datetime

    Declare @enddate datetime

    --Set @startdate ='12/01/2011'

    --set @enddate = '12/13/2011'

    SELECT @startdate = convert(varchar, (GETDATE()-12),110)

    SELECT @enddate = convert(varchar, GETDATE(),110)

  • Ajdba (12/13/2011)


    Hello,

    I am trying to replace set of hard-coded datetime with datetimes dynamically captured; but the result I am getting are not same, can somebody suggest what I am missing.

    Tnx

    Declare @startdate datetime

    Declare @enddate datetime

    --Set @startdate ='12/01/2011'

    --set @enddate = '12/13/2011'

    SELECT @startdate = convert(varchar, (GETDATE()-12),110)

    SELECT @enddate = convert(varchar, GETDATE(),110)

    Try this:

    SELECT @startdate = cast(datediff(d,0,GETDATE()-12) AS datetime)

    SELECT @enddate = cast(datediff(d,0,GETDATE()) AS datetime)

    This will be faster and should give you what you need.

    Jared

    Jared
    CE - Microsoft

  • Are you just looking for the first and last day on the currnet month?

    If so, maybe this will help:SELECT

    DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0),

    DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, -1, CURRENT_TIMESTAMP), 0))

    EDIT: Opps, never mind.. I got dyslexic and ready 13 as 31.. 🙂

  • Thanks so much guys 🙂 I did get the expected result. Appreciate so much for your sincerity and devotion to the SQL Server community!

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

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