How to cast/convert DOB =""21NOV1974:00:00:00"" to sql 2008 Date datatype

  • How to cast/convert client supplied DOB =""21NOV1974:00:00:00"" to sql 2008 Date datatype

    eg:- need the above in this YYYY-MM-DD (1974-11-21) DATE format , inorder to append this info in the DOB field with (SQL 2008) Date datatype

    DECLARE @dt datatype = "21NOV1974:00:00:00"

    SELECT@dt AS [varDateTime],

    CAST(@dt AS DATE) AS [varDate]

  • Hello,

    If your input parameter has a space between date and time parts, then your convert function will work quite well

    DECLARE @dt varchar(50) = '21NOV1974 00:00:00'

    SELECT @dt AS [varDateTime], CAST(@dt AS DATE) AS [varDate]

    If your source does not have this space, it is better to add it by replacing the first ":" or simply splitting the day,month and year parts.

    Or you can try the following statement with your original input variable value

    SELECT CAST(LEFT(@dt, CHARINDEX(' ',REPLACE(@dt,':',' '),0)-1) AS DATE) AS [varDate]

    Eralper

    SQL Server 2008 & T-SQL

  • Thanks Mate i'll give it a go.

    Appreciate ur help.

Viewing 3 posts - 1 through 2 (of 2 total)

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