Error using built in function SwitchOffset

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spoonybard
    New Member
    • Nov 2007
    • 19

    Error using built in function SwitchOffset

    Hello,

    I am trying to run a query using the SwitchOffset built in function to modify the timezone on a DatetimeOffset field.

    I am receiving the following error: "The timezone provided to builtin function switchoffset is invalid."

    Here is the query:
    Select SWITCHOFFSET(La stFailedOn, Cast((GMTOFFSET * 60) as int)
    from MyTable

    Note: LastFailedOn is of DatetimeOffset type and GMTOFFSET is of type float.

    Also, the range of GMTOFFSET is between -10 and 11 with no NULLs.

    Finally, when I replace the time_zone attribute with an integer (ex: -300), the query works no problem. I have tested the time_zone calculation on my data and all values returned are valid signed integers within the valid GMTOFFSET range.

    Thanks,

    Jason
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    The time_zone variable should either be a string or an integer, not float.

    Read more here


    Happy coding!

    -- CK

    Comment

    • spoonybard
      New Member
      • Nov 2007
      • 19

      #3
      To address the time_zone data type comment, I am casting our result to an integer, so there is no problem with the value being a float.

      With further testing, I have discovered that by adding an ORDER BY clause to the end of the query, that no error occurs. Also, If I select the one row that has data and one row that does not, it works fine. So does selecting a group of specific rows.

      Here is the revised query:
      Select SWITCHOFFSET(La stFailedOn, Cast((GMTOFFSET * 60) as int)
      from MyTable
      ORDER BY MyTable.Primary Key

      Note: The Primary Key is a Clustered Index. So not only is the result set the same, it is most likely returned in the same order.

      Thanks.

      Comment

      Working...