SQLTeam.com | Weblogs | Forums

Behavior of CONVERT

sql2008r2

#1

Hello, I put this in the Corral as it is not really a question...
I was concatenating to create a unique ID by adding a DATETIME value to a given CHAR value. This went well. I then wanted to add some archive data which used DATE and TIME. Well I was surprised when during a test the values were okay even though I thought I would need more conversions/manipulations.
Example:

CREATE TABLE #myTemp (RecDate DATE, RecTime TIME);
INSERT INTO #myTemp VALUES 
    ('2012-05-04','10:11:20.0000000'),
    ('2012-05-04','08:19:09.0000000'),
    ('2012-05-04','11:09:29.0000000'); 

-- style 120 --- yyyy-mm-dd hh:mi:ss(24h)
SELECT P.RecDate, CONVERT(VARCHAR(20), P.RecDate, 120), P.RecTime, CONVERT(VARCHAR(20), P.RecTime, 120), CONVERT(VARCHAR(20), P.RecDate, 120) + ' ' + CONVERT(VARCHAR(20), P.RecTime, 120) 
FROM #myTemp P; 

DROP TABLE #myTemp; 

I though the time would add a date and the date add a time. Note I used code from converting the DATETIME and had not changed it yet. At this point I found I did not need to make changes to the code.

Just thought I would see if anyone else had similar "it was less complicated than I thought".