Author |
Topic |
sql2020
Yak Posting Veteran
54 Posts |
Posted - 2010-06-03 : 08:57:13
|
I have a coloumn submitdate in epoch time. but i want to display normal for reports. please tell me how to convert that.sql2020 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-06-03 : 09:35:06
|
If it's like a java timestamp (a count of milliseconds from a set time) then this is the function I wrote to do the job.CREATE FUNCTION [dbo].[getJavaTimeStampAsDate] (@timeStamp BIGINT) RETURNS DATETIMEAS BEGIN /*** GetJavaTimeStampAsDate ************************************************** * * Takes a Java timestamp (a bigint count of miliseconds from 1970-01-01) * Returns a datetime * * Charlie (2010-01-19) * * Splits the timestamp using integer division into a number of: * days, seconds and miliseconds and then adds them one after another * to the epoch to achieve the date * * A SQL INT is a 4 byte signed integer so the maximum number of days will * be 2,147,483,647 which is 5,879,489 years. A DATETIME can only hold * dates up until 9999-12-31 so we are well covered for scale. Charlie * *****************************************************************************/ DECLARE @epoch DATETIME DECLARE @return DATETIME DECLARE @days INT DECLARE @seconds INT -- Set the Epoch (standard java epoch is 1st Jan 1970 at 00:00:00.000 SELECT @epoch = '19700101' -- If @timeStamp is NULL return epoch to avoid problems IF @timeStamp IS NULL RETURN @epoch -- No. of whole days in the timestamp and truncate timestamp SELECT @days = @timeStamp / 1000 / 60 / 60 / 24 SELECT @timeStamp = @timeStamp - CAST(@days AS BIGINT) * 24 * 60 * 60 * 1000 -- No of whole seconds in the remainder and truncate SELECT @seconds = @timeStamp / 1000 SELECT @timeStamp = @timeStamp - CAST(@seconds AS BIGINT) * 1000 -- Add days, remainder seconds and finally remainder miliseconds to epoch SELECT @return = DATEADD(DAY, @days, @epoch) SELECT @return = DATEADD(SECOND, @Seconds, @return) SELECT @return = DATEADD(MILLISECOND, @timeStamp, @return) -- Return the date RETURN @returnEND If that's not what you need (to convert milisecond count from an epoch startdate to a DATETIME) then give some sample data and required output.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
|
|
sql2020
Yak Posting Veteran
54 Posts |
Posted - 2010-06-03 : 09:39:45
|
HiAny other type to convertsql2020 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-06-03 : 09:44:53
|
quote: HiAny other type to convertsql2020
What do you mean?Post your sample data and required output. I have no idea what you mean by this.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
|
|
sql2020
Yak Posting Veteran
54 Posts |
Posted - 2010-06-03 : 10:16:54
|
'1120073114' this is the field i want to convert like '2005-06-29 15:25:14'sql2020 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-06-03 : 10:25:48
|
Ok -- so it's a count of SECONDS from 1970-01-01T00:00:00.Try thisSELECT DATEADD(SECOND, 1120073114, '19700101') And obviouslySELECT DATEADD(SECOND, <yourColumn>, '19700101') AS [converted DateTime]FROM <theTable> Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2010-06-03 : 13:41:27
|
You're hoping that by the time we get to SQL Server 2038 DATEADD will accept 64-bit integers? |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2010-06-03 : 14:05:24
|
quote: Originally posted by Transact Charlie If it's like a java timestamp (a count of milliseconds from a set time) then this is the function I wrote to do the job.CREATE FUNCTION [dbo].[getJavaTimeStampAsDate] (@timeStamp BIGINT) RETURNS DATETIMEAS BEGIN /*** GetJavaTimeStampAsDate ************************************************** * * Takes a Java timestamp (a bigint count of miliseconds from 1970-01-01) * Returns a datetime * * Charlie (2010-01-19) * * Splits the timestamp using integer division into a number of: * days, seconds and miliseconds and then adds them one after another * to the epoch to achieve the date * * A SQL INT is a 4 byte signed integer so the maximum number of days will * be 2,147,483,647 which is 5,879,489 years. A DATETIME can only hold * dates up until 9999-12-31 so we are well covered for scale. Charlie * *****************************************************************************/ DECLARE @epoch DATETIME DECLARE @return DATETIME DECLARE @days INT DECLARE @seconds INT -- Set the Epoch (standard java epoch is 1st Jan 1970 at 00:00:00.000 SELECT @epoch = '19700101' -- If @timeStamp is NULL return epoch to avoid problems IF @timeStamp IS NULL RETURN @epoch -- No. of whole days in the timestamp and truncate timestamp SELECT @days = @timeStamp / 1000 / 60 / 60 / 24 SELECT @timeStamp = @timeStamp - CAST(@days AS BIGINT) * 24 * 60 * 60 * 1000 -- No of whole seconds in the remainder and truncate SELECT @seconds = @timeStamp / 1000 SELECT @timeStamp = @timeStamp - CAST(@seconds AS BIGINT) * 1000 -- Add days, remainder seconds and finally remainder miliseconds to epoch SELECT @return = DATEADD(DAY, @days, @epoch) SELECT @return = DATEADD(SECOND, @Seconds, @return) SELECT @return = DATEADD(MILLISECOND, @timeStamp, @return) -- Return the date RETURN @returnEND If that's not what you need (to convert milisecond count from an epoch startdate to a DATETIME) then give some sample data and required output.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Here is my function for the same thing:create function [dbo].[F_JAVATIME_TO_DATETIME] ( @JAVATIME bigint )returns datetimeas/*Function: F_JAVATIME_TO_DATETIME Finds SQL Server datetime from JAVA time. JAVA time is the difference in milliseconds from 1970-01-01 00:00:00.000 UDT. Valid @JAVATIME range is -6847804800001 through 253402300799998. Return null if @JAVATIME is outside this range.*/beginif @JAVATIME between -6847804800001 and 253402300799998 begin return dateadd(ms,@JAVATIME%86400000,(@JAVATIME/86400000)+25567) endreturn nullend CODO ERGO SUM |
|
|
sql2020
Yak Posting Veteran
54 Posts |
Posted - 2010-06-04 : 16:03:37
|
Can you post same query to convert GMT -5 format Means Epoch to GMT -5 formatsql2020 |
|
|
sql2020
Yak Posting Veteran
54 Posts |
Posted - 2010-06-04 : 16:03:37
|
Can you post same query to convert GMT -5 format Means Epoch to GMT -5 formatsql2020 |
|
|
sql2020
Yak Posting Veteran
54 Posts |
Posted - 2010-06-04 : 16:03:37
|
Can you post same query to convert GMT -5 format Means Epoch to GMT -5 formatsql2020 |
|
|
sql2020
Yak Posting Veteran
54 Posts |
Posted - 2010-06-04 : 16:03:37
|
Can you post same query to convert GMT -5 format Means Epoch to GMT -5 formatsql2020 |
|
|
sql2020
Yak Posting Veteran
54 Posts |
Posted - 2010-06-04 : 16:03:37
|
Can you post same query to convert GMT -5 format Means Epoch to GMT -5 formatsql2020 |
|
|
sql2020
Yak Posting Veteran
54 Posts |
Posted - 2010-06-07 : 02:56:27
|
Can you post same query to convert GMT -5 format Means Epoch to GMT -5 formatsql2020 |
|
|
sql2020
Yak Posting Veteran
54 Posts |
Posted - 2010-06-07 : 03:11:56
|
Actually problem is time convertionthis is Epoch time: 1262887606when i use "DATEADD(SECOND, 1120073114, '19700101')" this it display time is : 2010-01-07 18:06:46.000Actually i want this time : 2010-01-07 13:06:46where i modify for this result. please tell me.sql2020 |
|
|
sql2020
Yak Posting Veteran
54 Posts |
Posted - 2010-06-07 : 03:17:48
|
Actually problem is time convertionthis is Epoch time: 1262887606when i use "DATEADD(SECOND, 1120073114, '1970-01-01 00:00:00')" this it display time is : 2010-01-07 18:06:46.000Actually i want this time : 2010-01-07 13:06:46where i modify for this result. please tell me.sql2020 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-07 : 03:47:03
|
quote: Originally posted by sql2020 Actually problem is time convertionthis is Epoch time: 1262887606when i use "DATEADD(SECOND, 1120073114, '1970-01-01 00:00:00')" this it display time is : 2010-01-07 18:06:46.000Actually i want this time : 2010-01-07 13:06:46where i modify for this result. please tell me.sql2020
If you need 5 hours less than a actual time,select DATEADD(hour,-5,DATEADD(SECOND, 1262887606, '1970-01-01 00:00:00'))MadhivananFailing to plan is Planning to fail |
|
|
sql2020
Yak Posting Veteran
54 Posts |
Posted - 2010-06-07 : 05:40:56
|
Hi this i got result 2010-01-07 18:06:46.000 but i want 2010-01-07 18:06:46 how to remove last 3 zerossql2020 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-07 : 06:04:06
|
quote: Originally posted by sql2020 Hi this i got result 2010-01-07 18:06:46.000 but i want 2010-01-07 18:06:46 how to remove last 3 zerossql2020
It is a formation issue that you need to do it in your front end application. Otherwiseselect convert(varchar(20),DATEADD(SECOND, 1262887606, '1970-01-01 00:00:00'),20) as datesMadhivananFailing to plan is Planning to fail |
|
|
|