sqlsql-serverdatetimesql-convert

How to convert DateTime to VarChar


I need to convert a value which is in a DateTime variable into a varchar variable formatted as yyyy-mm-dd format (without time part).

How do I do that?


Solution

  • With Microsoft Sql Server:

    --
    -- Create test case
    --
    DECLARE @myDateTime DATETIME
    SET @myDateTime = '2008-05-03'
    
    --
    -- Convert string
    --
    SELECT LEFT(CONVERT(VARCHAR, @myDateTime, 120), 10)