Profile

Wednesday, 6 July 2011

SQL: Converting Data Type

Sometime data type needs to be changed for querying or BI reporting use. In situations like this the CONVERT clause comes in handy.

For example, I need to grep the data of Date for reporting. However the data type of date in the data warehouse are Date time which has date and time stamp.

I don't need the time stamp part of it. I want to turn my date key from datetime to just date, so I place a CONVERT clause within my SELECT statement. 



SELECT
CONVERT (DATE, FullDateAlternateKey) AS FullDate
FROM AdventureWorksDW.dbo.DimTime

After the query is executed, notice the difference of data type between the original table and the query output: 



MSDN has a full list of data type which CONVERT can handle. Please check out this link:

http://msdn.microsoft.com/en-us/library/ms187928.aspx

No comments: