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
Solved

SQL SWITCHOFFSET fuction does not like offset returned by timezoneinfo

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

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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦

838 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