Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
Hello,

I have a value in bigint and i need to convert it into datetime my value is this "381403800" i tried these solutions but not a single solution is working... its correspond value is '1982-02-01 09:30:00.000'.



SQL
SELECT DATEADD(SECOND, 381403800 / 1000, '19691231 20:00')

select dateadd(hh,-4,dateadd(ss,381403800/1000,'1970/01/01 00:00'))

select CONVERT(DATETIME,REPLACE(DATEADD( day, 381403800, '12/31/1849'),'12/31/1849',0),101)

SELECT DateAdd(ms, 381403800 % 1000, DateAdd(SECOND, 381403800 /  1000, '1970-01-01T00:00:00.000')) AS ToTheMs,
   DateAdd(SECOND, 381403800 /  1000, '1970-01-01T00:00:00.000')  AS ToTheSecond,
   DateAdd(MINUTE, 381403800 / 60000, '1970-01-01T00:00:00.000')  AS ToTheSecond
Posted
Updated 25-Sep-14 19:26pm
v2
Comments
george4986 26-Sep-14 1:34am    
can u plz tell me how "381403800" is related to "1982-02-01 09:30:00.000" ?
srishti_ 26-Sep-14 2:32am    
i don't know i have a XML file and corresponding HTML showing this value.
george4986 26-Sep-14 2:59am    
can u post the xml structure and ur html conversion code here?
srishti_ 26-Sep-14 3:29am    
I am not converting XML to HTML its a government file both XML and their corresponding HTML .I am only insert that XML data to my database for that i need that big int conversion to date time that's it
george4986 26-Sep-14 4:39am    
what about other values ,does they also differ in xml and html?
are u sure no encryption is used in xml?

1 solution

Cool problem!
Fire this up in your SQL manager, lore and behold

select convert(datetime, 0)
 select convert(bigint, convert(datetime, '19820201'))
 -- 29981 equivalates to 1982-02-01 NOT 381403800
 select convert(bigint, getdate())
 -- today is 41906
 select convert(bigint,dateadd(day, 1, getdate()))
 -- tomorrow is 41907
 declare @later int;
 set @later = 381403800 - convert(bigint, getdate())
 --select dateadd(day, @later, getdate()) would get overflow on the date type
 declare @oneTenth int;
 set @oneTenth = @later / 1000
 select dateadd(day, @oneTenth, getdate())
 -- so 1/1000 of the date you're reqesting is 3058-11-13
 -- which is 
 select datediff(day, getdate(), convert(datetime, '3058-11-13'))
 -- 381361 days from now or in rougly 1044 years, adding the zeroes you get 1.044.000 years
 --so in a million years all that we know is considered to be over, including mssql?
 -- your date is year 1046014 bc :)


wonder where you got that date from :)

in dot net you would get the date from that tick of 01-01-0001 00:00:38
this is intreguingly close to the dotnet value DateTime.Min (01-01-0001 00:00:00)

C#
var dt = DateTime.MinValue.AddSeconds(38); //new DateTime(381403800);
            System.Diagnostics.Debug.WriteLine(dt.Ticks);

you get a value almost equal to the one you have

What you got to do is to check if the date being converted is below the minimum value the sql server can hold and then set it to null or the minimally supported value, see the first convert
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900