Convert Nvarchar to Datetime

  • Hi everyone,

    I have an nvarchar(20) column containing a date and time in the format (DD\MM\YYYY HH:MM:SS). That value actually needs to populate a datetime column in another table.

    Is there a simple way of doing that conversion??

  • 
    
    DECLARE @d nvarchar(20)
    SET @d = '21/12/2003 15:22:15'
    SELECT CONVERT(datetime,@d,103)

    --Jonathan



    --Jonathan

  • Thanks Jonathan,

    Your script works and I'd tried the convert on several individual dates in that column and it works, but if I run the query against all of the data in the column I get the error

    Syntax error converting datetime from character string.

    I guess there must be some data not in the datetime format in there so will have to pick through and find it!! Cheers.

  • quote:


    I guess there must be some data not in the datetime format in there so will have to pick through and find it!! Cheers.


    Rather than "pick though it," try something like this:

    
    
    SET DATEFORMAT dmy
    SELECT KeyCol, DateCol
    FROM YourTable
    WHERE ISDATE(DateCol) = 0

    --Jonathan



    --Jonathan

  • quote:


    SET DATEFORMAT dmySELECT KeyCol, DateColFROM YourTableWHERE ISDATE(DateCol) = 0


    Jonathan,

    Depends on language settings, eg

    English

    2003-11-15 1

    2003-15-11 0

    15/11/2003 0

    11/15/2003 1

    British English

    2003-11-15 0

    2003-15-11 1

    15/11/2003 1

    11/15/2003 0

    Far away is close at hand in the images of elsewhere.
    Anon.

  • quote:


    quote:


    SET DATEFORMAT dmySELECT KeyCol, DateColFROM YourTableWHERE ISDATE(DateCol) = 0


    Jonathan,

    Depends on language settings, eg

    English

    2003-11-15 1

    2003-15-11 0

    15/11/2003 0

    11/15/2003 1

    British English

    2003-11-15 0

    2003-15-11 1

    15/11/2003 1

    11/15/2003 0


    David,

    Look again at my script and the OP's date format.

    --Jonathan



    --Jonathan

  • Thanks for your help guys. I had got it right originally in the way Jonathan's first script shows, using convert. The thing that threw me and made me think it wasn't working was that there were a few zero length strings in the original nvarchar column. Trying to do a convert on these threw up the error.

  • Oops!

    My apologies

    I hate dates in sql

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Why, David?

    Datetime data types help us solving many problems we wouldn't have without them.

    I remember back in the 'good old dbase days' everybody I knew had a conversion routine to transform such information into yyyymmdd. Works really fine. But today we are significantly further ahead ?!?

    Oh happy day

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • quote:


    Oops!

    My apologies

    I hate dates in sql


    I don't. I think I'll write an article on them. The one on this site (Syverson) is poor, and I've seen a lot of confusion in these forums. I remember consulting at one site where the developers told me that their policy was to use the char data type for dates because SQL Server "does not handle dates correctly."

    --Jonathan



    --Jonathan

  • quote:


    I hate dates in sql


    Hey! Come on chaps, a bit tongue in cheek here.

    I do all my data processing in yyyy-mm-dd format and only convert to/from dd/mm/yyyy for presentation/input.

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Glad to see a basic question sparked a bit of healthy debate!

  • quote:


    quote:


    I hate dates in sql


    Hey! Come on chaps, a bit tongue in cheek here.

    I do all my data processing in yyyy-mm-dd format and only convert to/from dd/mm/yyyy for presentation/input.


    Guess we need a tongue in cheek smiley.

    I'm with you on always using the ODBC canonical or the ISO formats in code.

    --Jonathan



    --Jonathan

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply