Search code examples
sql-serversql-server-2000

Fetch, split, re-arrange, output


I have dates stored as strings in a SQL Server 2000 database like this:

01/01/2017

I need to fetch, split, re-arrange and output it to a .csv like this:

2017-01-01

I have been playing with REPLACE and CONVERT functions but none seems to do what I need it to do. Any suggestions would be helpful.


Solution

  • This should do the trick...

    DECLARE @DateString VARCHAR(15) = '1/1/2015';
    
    SELECT ReformattedDate = CONVERT(CHAR(10), CAST(@DateString AS DATETIME), 21);
    

    returns...

    ReformattedDate
    ---------------
    2015-01-01