[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1057
  • Last Modified:

SQL SWITCHOFFSET fuction does not like offset returned by timezoneinfo

I am using timezoneinfo to return an offset to parse into an sql string

SQL SWITCHOFFSET fuction did not like two things as Invalid:
1) the single digit hour and
2) the seconds like this: -6:00:00

This is okay -06:00.

I can manipulate and format it by parsing, but that seems messy. Can you offer any built-in direction?

Here is the code:

Dim tz As TimeZoneInfo
Dim tsUTZOffset As TimeSpan
Dim thisOffset  as String

 tz = TimeZoneInfo.FindSystemTimeZoneById(strTZID)
 tsUTZOffset = tz.GetUtcOffset(DateTime.UtcNow)

thisOffset = tsUTZOffset .Hours.ToString  ' (returns -6:00:00)

 strSQL = "SELECT CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset,  myDate), '" & thisOffset & " '))..."

Open in new window

0
hitman3030
Asked:
hitman3030
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
http://msdn.microsoft.com/en-us/library/bb677244.aspx
time_zone
    Is a character string in the format [+|-]TZH:TZM or a signed integer (of minutes) that represents the time zone offset, and is assumed to be daylight-saving aware and adjusted.


so, you have no choice and pass only HH:MI format.

you could do this:
.Hours.ToString("HH:mm")

and you are done without parsing
0
 
hitman3030Author Commented:
That was close. Can't use the .Hours. Also have to add the +/- value. Here is final solution:


            If tsUTZOffset.Hours = 0 Then
                thisOffset  = "+00:00"
            Else
                thisOffset  = tsUTZOffset.ToString("hh\:mm")
            End If

            If tsUTZOffset.Hours > 0 Then
                thisOffset  = "+" & thisOffset  
            Else
                thisOffset  = "-" & thisOffset  
            End If
 

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now