Monday, March 31, 2014

Converting string dates from a table

------------
-- Converting string dates from a table
------------

-- Create and populate a test table with a string date
USE tempdb;
SELECT
      DepartmentID,
      LastUpdate=CONVERT(varchar,
                 dateadd(day, DepartmentID, ModifiedDate),100)
INTO DeptInfo
FROM AdventureWorks.HumanResources.Department

SELECT * FROM DeptInfo
/* Partial results

DepartmentID      LastUpdate
1                       Jun  2 1998 12:00AM
2                       Jun  3 1998 12:00AM
*/

-- Convert string date column to datetime
SELECT
      DepartmentID,
      LastChangeDate=convert(datetime, LastUpdate)
FROM DeptInfo
/* Partial results

DepartmentID            LastChangeDate
1                       1998-06-02 00:00:00.000
2                       1998-06-03 00:00:00.000
*/
DROP TABLE DeptInfo

GO

No comments:

Post a Comment