Results 1 to 7 of 7

Thread: char to date in sql server

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2005
    Posts
    58

    char to date in sql server

    Hi all

    Im using sql server-2000 and storing date in varchar data type in dd/mm/yyyy format. how can i convert back to date in dd/mm/yyyy format

    Thanks

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: char to date in sql server

    Quote Originally Posted by searchajay
    Hi all

    Im using sql server-2000 and storing date in varchar data type in dd/mm/yyyy format. how can i convert back to date in dd/mm/yyyy format

    Thanks
    You must have a really good reason to store a date in VARCHAR format...

    CONVERT function will convert most data formats from one type to another, and there are many date formats supported. Checks SQL Books Online for details...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2005
    Posts
    58

    Re: char to date in sql server

    Hi

    Its giving me error when i execute following query

    select convert(datetime,bookdate) from vwVatReturn
    or
    select convert(smalldatetime,bookdate) from vwVatReturn



    The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: char to date in sql server

    I just did this in query analyzer

    Code:
    Declare @DateString varchar(10)
    Declare @DateVbl datetime
    Set @DateString='12/31/2005'
    Print 'String='+@DateString
    Set @DateVbl=Convert(Datetime,@DateString)
    Print @DateVbl
    Set @DateVbl=Cast(@DateString as datetime)
    Print @DateVbl
    And got this in the results panel...

    Code:
    String=12/31/2005
    Dec 31 2005 12:00AM
    Dec 31 2005 12:00AM
    The dates show as Dec 31 2005 because that's the way the PRINT T-SQL command works - but they are in the datetime variable properly...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2005
    Posts
    58

    Re: char to date in sql server

    Hi szlamany

    This works fine
    Declare @DateString varchar(10)
    Declare @DateVbl datetime
    Set @DateString='04/19/2004'
    Print 'String='+@DateString
    Set @DateVbl=Convert(Datetime,@DateString)
    Print @DateVbl
    Set @DateVbl=Cast(@DateString as datetime)
    Print @DateVbl

    BUT THIS WILL GENERATE ERROR


    Declare @DateVbl datetime
    Set @DateString='19/04/2004'
    Print 'String='+@DateString
    Set @DateVbl=Convert(Datetime,@DateString)
    Print @DateVbl
    Set @DateVbl=Cast(@DateString as datetime)
    Print @DateVbl

    I mean to say if you use mm/dd/yyy format it works fine, but if you use dd/mm/yyyy than it will generate error


    Thanks

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: char to date in sql server

    That is because (like most/all DBMS's) SQL Server works internally in american date format, and as far as I am aware there is no way to switch it to UK format

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2005
    Posts
    58

    Re: char to date in sql server

    Hi

    Thanks, so i will change the format accordingly

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width