Results 1 to 3 of 3

Thread: convert varchar to datetime

  1. #1
    Join Date
    Nov 2005
    Posts
    4

    convert varchar to datetime

    I have a hard time converting a varchar field with a string like "20051123" to a datetime value into a new field.

    Test SQL:

    stringdate is a varchar field with a string like "20051123"nin table tableval:

    select CONVERT(datetime, stringdate, 103) as newdate from tableval

    select CAST( stringdate as datetime ) as newdate from tableval

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

    Please help.

    Thanks

    amy

  2. #2
    Join Date
    Dec 2004
    Posts
    502
    That just means that you have some data that are not valid SQL Server dates. Try this query to find the bad data:

    SELECT stringdate FROM tableval
    WHERE ISDATE(stringdate) = 0

  3. #3
    Join Date
    Nov 2005
    Posts
    4
    Thank you nosepicker. I will try it.

    amy

Posting Permissions

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