datetime conversion

  • Hi,

    I need some help with SQL.

    I want to convert a datetime value into a string, however, when I use the CAST function, it returns, eg,  Jan 1 1900, when I actually want '1900-01-01' with the quotes.

    Any ideas?

    tks.

     

  • Convert and string concatenation can help you.

  • try this

     

    DECLARE @YEAR CHAR(4), @MONTH VARCHAR(2), @DAY VARCHAR(2)

    SET @YEAR = CAST(YEAR(GETDATE()) AS CHAR(4))

    SET @MONTH = CAST(MONTH(GETDATE()) AS VARCHAR(2))

    IF LEN(@MONTH) = 1 SET @MONTH = '0' + @MONTH

    SET @DAY = CAST(DAY(GETDATE()) AS VARCHAR(2))

    IF LEN(@DAY) = 1 SET @DAY = '0' + @DAY

    SELECT '''' + @YEAR + '-' + @MONTH + '-' + @DAY + '''' AS DATE

  • Actually, I think you're using the wrong function... in most cases, CAST and CONVERT work the same way...
    ...except for dates and then CONVERT works very nicely for converting DateTime values to strings.
    CONVERT looks like this...
     
    CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
       (see "Books-on-Line" for a table of what "style" a datetime expression can be when converting DateTime "expressions" to CHAR or VARCHAR)
     
    To convert to the format that you want, try this...
    CONVERT(Varchar(30), yourdatetimevalue, 120)  but that will also give you the time 
     
    But, that's not a problem either...
    LEFT(CONVERT(Varchar(30), yourdatetimevalue, 120),10) but that doesn't give you the quotes you want
     
    ''''+LEFT(CONVERT(Varchar(30), yourdatetimevalue, 120),10)+'''' should do the trick
     
    And, "YES", those are four single quotes.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I tried the CONVERT function, and it didn't seem to work...

    Thanks Jeff, your last suggestion, this works great.

     

  • Statement below does it "in one".  The part shown in red is the date to convert to the format you want. Change the whole of the portion shown in red by your datetime variable - don't be put off by it being an expression in my sample code - I could have used a separate "declare" and "set".

    select ''''+substring(convert(varchar,convert(datetime,'1 Feb 1990'),120),1,10)+''''
    '1990-02-01'
  • How about this, we can get rid of the substring as well.

    select ''''+convert(varchar(10),convert(datetime,'1 Feb 1990'),120)+''''

    Or say for current day

     

    select ''''+convert(varchar(10),convert(datetime,GETDATE()),120)+''''

     

     

    Prasad Bhogadi
    www.inforaise.com

  • try select '''' + convert(varchar(10),getdate(),120) + ''''

     

    or

    select  convert(varchar(10),getdate(),120)  to get the date without quotes

  • Yeah should absolutely solve it without any issues whatsoever.

     

    Prasad Bhogadi
    www.inforaise.com

  • Sql Server also a neat function QuoteName() that can help clean up all the single quotes.  You could use Select QuoteName(convert(varchar(10),getdate(),120), '''')which will return '2004-03-03'.  It is a little easier to read.

Viewing 10 posts - 1 through 9 (of 9 total)

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