Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Kat writes "Hello,How do you convert an INT (such as 124155) to a DATETIME?That is, 124155 = 12:41 pm and 55 seconds.How do I convert this to just the TIME portion of the datetime?So far:DECLARE @myval intSET @myval = 124155SELECT CAST(@myval AS datetime)Yields:2239-12-05 00:00:00.000Which is obviously wrong. I don't know if using DATEPART would help, but somehow I don't think so.If anyone can guide me, very appreciated!Thanks :-)"
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2005-08-02 : 11:11:48
Why do you want this conversion?You have to use proper datatype to store the dataMadhivananFailing to plan is Planning to fail
EDIT: Added fix for less-than 6 digit datesKristen
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2005-08-03 : 01:38:49
Kristen, this will work only if the value has six digits >>How do I convert this to just the TIME portion of the datetime?What date do you want it to be part of?MadhivananFailing to plan is Planning to fail
Kristen
Test
22859 Posts
Posted - 2005-08-03 : 04:49:39
"this will work only if the value has six digits"Good catch Mad.I should have also added that the "time" will be made part of "01-Jan-1900", this is how SQL chooses to store just-time at this moment - but, for example, if you have two such "time" records DIFFERENCE etc. will work just fineKristen