Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « How To Script Out The Whole Database In SQL Server 2005 and SQL Server 2008Can't depend on sp_depends? Try using sp_refreshsqlmodule »
    comments

    I answered this question today and thought it would be useful to create a little post

    If you have a varchar value like this '2009-06-26 14:30:00.000Z+4:30' you want to take 4 hours and 30 minutes and then subtract that from the date itself so in this case you will get 2009-06-26 11:00:00.000. The reason you subtract is because the +4:30 means that this was generated in a zone that is 4:30 ahead of utc

    So first we need to figure out a couple of things
    1) where are the minutes?
    2) where is the hour?
    3) is it positive or negative?

    Here are the answers
    1) where are the minutes?
    The minutes are the last 2 characters

    1. declare @date varchar(100)
    2. select @date = '2009-06-26 14:30:00.000Z+4:30'
    3.  
    4. select right(@date,2)

    2) where is the hour?
    The hour starts after the Z and last for 2 or 3 characters including the sign, we will just grab 3 and replace : with an empty string

    1. declare @date varchar(100)
    2. select @date = '2009-06-26 14:30:00.000Z+4:30'
    3. select replace(substring(@date,patindex('%z%',@date)+ 1,3),':','')
    4. go

    +4


    1. declare @date varchar(100)
    2. select @date = '2009-06-26 14:30:00.000Z-4:30'
    3. select replace(substring(@date,patindex('%z%',@date)+ 1,3),':','')
    4. go

    -4


    1. declare @date varchar(100)
    2. select @date = '2009-06-26 14:30:00.000Z+14:30'
    3. select replace(substring(@date,patindex('%z%',@date)+ 1,3),':','')

    +14

    3) is it positive or negative?
    That we already grabbed above for the hour, for the minute we need to do something like this

    1. declare @date varchar(100),@multiplier int
    2.  
    3. select @date = '2009-06-26 14:30:00.000Z+4:30'
    4. select  case when @date like '%+%' then -1 else 1 end

    We also need to convert the stuff we did above to integers in order to add

    So here is the complete code

    1. \
    2. declare @date varchar(100),@multiplier int
    3.  
    4. select @date = '2009-06-26 14:30:00.000Z+4:30'
    5. select @multiplier = case when @date like '%+%' then -1 else 1 end
    6.  
    7.  
    8. select dateadd(mi, @multiplier *convert(int,right(@date,2)),dateadd(hh
    9.     ,-1 * convert(int,replace(substring(@date,patindex('%z%',@date)+ 1,3),':',''))
    10.     ,left(@date,23)))
    11. go
    12.  
    13.  
    14. --2009-06-26 10:00:00.000
    15.  
    16. declare @date varchar(100),@multiplier int
    17.  
    18. select @date = '2009-06-26 14:30:00.000Z-4:30'
    19. select @multiplier = case when @date like '%+%' then -1 else 1 end
    20.  
    21. select dateadd(mi, @multiplier *convert(int,right(@date,2)),dateadd(hh
    22.     ,-1 * convert(int,replace(substring(@date,patindex('%z%',@date)+ 1,3),':',''))
    23.     ,left(@date,23)))
    24. go
    25.  
    26. --2009-06-26 19:00:00.000
    27.  
    28. declare @date varchar(100),@multiplier int
    29.  
    30. select @date = '2009-06-26 14:30:00.000Z+14:30'
    31. select @multiplier = case when @date like '%+%' then -1 else 1 end
    32.  
    33. select dateadd(mi, @multiplier *convert(int,right(@date,2)),dateadd(hh
    34.     ,-1 * convert(int,replace(substring(@date,patindex('%z%',@date)+ 1,3),':',''))
    35.     ,left(@date,23)))
    36. go
    37.  
    38. --2009-06-26 01:00:00.000
    39.  
    40.  
    41. declare @date varchar(100),@multiplier int
    42. select @date = '2009-06-26 14:30:00.000Z-14:30'
    43. select @multiplier = case when @date like '%+%' then -1 else 1 end
    44.  
    45. select dateadd(mi, @multiplier *convert(int,right(@date,2)),dateadd(hh
    46.     ,-1 * convert(int,replace(substring(@date,patindex('%z%',@date)+ 1,3),':',''))
    47.     ,left(@date,23)))
    48. go
    49.  
    50. --2009-06-27 05:00:00.000




    *** If you have a SQL related question try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2117 views
    Instapaper

    2 comments

    Comment from: niikola [Member] Email
    niikola Is it allowed to use datetime data type?
    06/30/09 @ 06:04
    Comment from: SQLDenis [Member] Email
    SQLDenis This was a response to a question where it cam in as a string, if you had a datetime hoiw would you pass in an offset? you would have to use a different param. But yes would make it much easier
    06/30/09 @ 08:07

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)