Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2003
    Location
    Longmont, CO
    Posts
    258

    Unanswered: Char to smalldatetime

    SQL Server 2000

    Used to know how to do this but am having no luck today. I have data coming in from a .txt file that gives me char strings for dates, e.g. 02242003 for Feb. 2, 2003.

    Need to whomp this into small datetime with the whole convert/cast thing but I guess I've previously only gone the other way--smalldatetime to char.

    Thanks!!!

  2. #2
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    declare @dString char(8)
    set @dString = '02242003'
    select cast(substring(@dString, 5, 4) +
    substring(@dString, 1, 4) as smalldatetime)

  3. #3
    Join Date
    Jun 2003
    Location
    Longmont, CO
    Posts
    258
    Ok, that's a good step in the right direction. Now, how do I get the result to be 02/24/2003?

    Tried Convert(smalldatetime, cast(substring(@dString, 5, 4) +
    substring(@dString, 1, 4) as smalldatetime), 101) and it barked at me.

  4. #4
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Code:
    declare @dString char(8)
    set @dString = '02242003'
    select CONVERT(varchar,cast(substring(@dString, 5, 4) + 
    substring(@dString, 1, 4) as smalldatetime),101)
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  5. #5
    Join Date
    Jun 2003
    Location
    Longmont, CO
    Posts
    258
    Brett,

    Sorry for not getting back to you. Thanks mucho! This was exactly what I needed.

    One bright spot in a lot of messy T-SQL writing!

Posting Permissions

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