?
Solved

SQL SWITCHOFFSET fuction does not like offset returned by timezoneinfo

Posted on 2013-01-21
2
Medium Priority
?
1,043 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

650 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