none
convert varchar to smalldatetime

    Question

  • A table contains a field of type smalldatetime
    The feed has this field as string i.e. '060830'
    Would like to insert this string date into the field of the table which has a datatype of smalldatetime.

    Is this the correct way to convert a string to smalldatetime?

    declare @DateFeed varchar(6)
    declare @TradeDate smalldatetime

    set @DateFeed = '060830'

    set @TradeDate = convert(smalldatetime, 6 + '-' + 8 + '-' + 30)
    print @TradeDate

    Thanks

    Thursday, July 12, 2007 7:49 AM

Answers

  • Use function CONVERT, with style 12. Let SQL Server decides the century (0 - 49 --> 2000 / 50 - 99 --> 1900).

     

    select convert(smalldatetime, '060830', 12)

    go

     

     

    AMB

    Thursday, July 12, 2007 1:07 PM

All replies

  • Use the following query...

    Prefixing the century value on your data & casting the result as datetime

     

    Code Snippet

     

    Select Cast('20' + '060830' as smalldatetime) 

     

     

    Thursday, July 12, 2007 7:56 AM
  • Use function CONVERT, with style 12. Let SQL Server decides the century (0 - 49 --> 2000 / 50 - 99 --> 1900).

     

    select convert(smalldatetime, '060830', 12)

    go

     

     

    AMB

    Thursday, July 12, 2007 1:07 PM