Solved

SQL SWITCHOFFSET fuction does not like offset returned by timezoneinfo

Posted on 2013-01-21
2
948 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now