Solved

SQL SWITCHOFFSET fuction does not like offset returned by timezoneinfo

Posted on 2013-01-21
2
969 Views
Last Modified: 2013-01-21
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
Comment
Question by:hitman3030
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38800155
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
 

Author Comment

by:hitman3030
ID: 38800328
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question