New Year's Eve

  • Nice, very nice, Steve. Happy New Year to all!

  • Aaron N. Cutshall (12/31/2013)


    L' Eomot Inversé (12/31/2013)


    The last second of 2013? How can that be on any day but Dec 31?

    I meant that the date in the variable could have been any day of the year since only the year portion was utilized. Of course, the last second of the year will be on Dec 31!;-)

    Aargh! I should have read your coment more carefully.

    Tom

  • Since the QotD is all about learning, allow me to point out a few things:

    First of all, none of these solutions really work...

    Msg 137, Level 15, State 2, Line 6

    Must declare the scalar variable "@ThisDate".

    Msg 137, Level 15, State 2, Line 7

    Must declare the scalar variable "@ThisDate".

    Msg 137, Level 15, State 2, Line 8

    Must declare the scalar variable "@ThisDate".

    Msg 137, Level 15, State 2, Line 9

    Must declare the scalar variable "@ThisDate".

    (when run on a case-sensitive collation, so change them all to be as defined: @thisdate)

    And our European friends, using the YDM format, will get this:

    Msg 242, Level 16, State 3, Line 5

    The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.

    To have this code work right, just use the ISO 8601 standard for dates/times:

    select @thisdate = '2013-12-31T00:00:00' -- dates with times - the milliseconds are optional

    --or

    select @thisdate = '20131231' -- just dates

    The advantage in using the ISO 8601 format is that it is an international standard with unambiguous specification. Also, this format is not affected by the SET DATEFORMAT or SET LANGUAGE setting.

    Ref: datetime

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • WayneS (12/31/2013)


    Since the QotD is all about learning, allow me to point out a few things:

    First of all, none of these solutions really work...

    Msg 137, Level 15, State 2, Line 6

    Must declare the scalar variable "@ThisDate".

    Msg 137, Level 15, State 2, Line 7

    Must declare the scalar variable "@ThisDate".

    Msg 137, Level 15, State 2, Line 8

    Must declare the scalar variable "@ThisDate".

    Msg 137, Level 15, State 2, Line 9

    Must declare the scalar variable "@ThisDate".

    (when run on a case-sensitive collation, so change them all to be as defined: @thisdate)

    And our European friends, using the YDM format, will get this:

    Msg 242, Level 16, State 3, Line 5

    The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.

    To have this code work right, just use the ISO 8601 standard for dates/times:

    select @thisdate = '2013-12-31T00:00:00' -- dates with times - the milliseconds are optional

    --or

    select @thisdate = '20131231' -- just dates

    The advantage in using the ISO 8601 format is that it is an international standard with unambiguous specification. Also, this format is not affected by the SET DATEFORMAT or SET LANGUAGE setting.

    Ref: datetime

    You could also just fix the code as follows:

    SET DATEFORMAT 'YMD'

    declare @ThisDate SMALLDATETIME = '2013/12/31'

    And to clarify a bit, it's the Instance collation and not just the database collation that will cause those issues Wayne mentioned.

    It should also be noted that Wayne fixed the second option otherwise his error message would have been different. :-D:-D;-)

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • SQLRNNR (12/31/2013)


    WayneS (12/31/2013)


    Since the QotD is all about learning, allow me to point out a few things:

    First of all, none of these solutions really work...

    Msg 137, Level 15, State 2, Line 6

    Must declare the scalar variable "@ThisDate".

    Msg 137, Level 15, State 2, Line 7

    Must declare the scalar variable "@ThisDate".

    Msg 137, Level 15, State 2, Line 8

    Must declare the scalar variable "@ThisDate".

    Msg 137, Level 15, State 2, Line 9

    Must declare the scalar variable "@ThisDate".

    (when run on a case-sensitive collation, so change them all to be as defined: @thisdate)

    And our European friends, using the YDM format, will get this:

    Msg 242, Level 16, State 3, Line 5

    The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.

    To have this code work right, just use the ISO 8601 standard for dates/times:

    select @thisdate = '2013-12-31T00:00:00' -- dates with times - the milliseconds are optional

    --or

    select @thisdate = '20131231' -- just dates

    The advantage in using the ISO 8601 format is that it is an international standard with unambiguous specification. Also, this format is not affected by the SET DATEFORMAT or SET LANGUAGE setting.

    Ref: datetime

    You could also just fix the code as follows:

    SET DATEFORMAT 'YMD'

    declare @ThisDate SMALLDATETIME = '2013/12/31'

    And to clarify a bit, it's the Instance collation and not just the database collation that will cause those issues Wayne mentioned.

    It should also be noted that Wayne fixed the second option otherwise his error message would have been different. :-D:-D;-)

    I thought it was understood that QotDs assume default SQL Server installation settings - if we veered off into region-specific settings, QotDs would have to have a dozen lines of code before you would get to the gist of the question.

  • Revenant (12/31/2013)


    I thought it was understood that QotDs assume default SQL Server installation settings - if we veered off into region-specific settings, QotDs would have to have a dozen lines of code before you would get to the gist of the question.

    +1

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Nice question to end the year with. I have had to monkey around with dates this way before so it seemed quite familiar to me!

  • Nice and Easy..!!

    But Why This (B) option: "select bdateadd( ss, -1, datediff(yy, 0, @ThisDate) + 1 )" Is it a Typing Mistake, or what.? 😉

  • Nice one, thanks!

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Revenant (12/31/2013)I thought it was understood that QotDs assume default SQL Server installation settings - if we veered off into region-specific settings, QotDs would have to have a dozen lines of code before you would get to the gist of the question.

    My SQL Server instance was installed with default settings, and has British date format. Presumably the default settings vary according to region?

    It was obvious that US format was assumed though, as there was no option for 'error' or 'none of the above'.

  • nice question..

    Thanks Steve.

  • nice and easy

  • I'm disappointed that the question used the "datetime" data type. This has clearly been replaced by Date, Time and Datetime2. Obviously some of the established forum members can't move on.

    Regards

    David

  • I agree about moving forward with the newer temporal data types, but the old datetime has some nice features, like zero as an int will implicitly cast to 1900-01-01 00:00:00. The new ones don't do that. It's also frustrating that you can't do things like @MyDatetime2 = @MyDate + @MyTime; a glaring oversight if you ask me.

    Sincerely,
    Daniel

Viewing 14 posts - 16 through 28 (of 28 total)

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