Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Convert datetime to just time (only hour)

Author  Topic 

notes4we
Yak Posting Veteran

90 Posts

Posted - 2008-09-26 : 16:00:51
Hello,

I have one datetime column named Hours in SQL Server Table1, which gives the output as 09/26/2008 01:00:00, 09/26/2008 02:00:00, 09/26/2008 03:00:00 till 09/26/2008 12:00:00 format.

I wanted to get just the year in int datatype from this format and I got it using (SELECT CONVERT(int, YEAR (Hours)) AS Year FROM Table1) and I am getting the correct Year as I wanted 2008. Same way I am getting month and date also. But the issue is with hour.

I want the hour also in the integer format. I mean if there is 01:00:00, then the output should just be 1 and same way the next should be 2.

Can anyone please help me to achieve this output?
I want a proper syntax for the same.
Meanwhile I will carry on the research on my part.
Thank you.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-26 : 16:51:47
DATEPART(YEAR, Col1)
DATEPART(MONTH, Col1)
DATEPART(DAY, Col1)
DATEPART(HOUR, Col1)



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

notes4we
Yak Posting Veteran

90 Posts

Posted - 2008-09-29 : 09:16:08
Thank you so much Peso. It is simple, but I was not able to understand how to acheive it.
Thank you.........
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-29 : 10:48:14
SELECT DATEADD(YEAR, DATEDIFF(YEAR, 0, Hours), 0)
FROM Table1



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -