is there a function to convert datetime to bigint in sql? E.g. I have a date "2010-11-02 00:00:00" and i would like to get result as number "20101102000000"
Or do I need to write some custom function which will remove -,space,: from datetime?
is there a function to convert datetime to bigint in sql? E.g. I have a date "2010-11-02 00:00:00" and i would like to get result as number "20101102000000"
Or do I need to write some custom function which will remove -,space,: from datetime?
you can try this code here..
update Table_1 set xnew=REPLACE(REPLACE(REPLACE (CONVERT(VARCHAR,x,20) , '-' , '' ),':',''),' ','')
but this is duplicate question I answered it here too : Sql datatype conversion
The solution I have been using lately (in SQL 2008 R2) is:
cast(CONVERT(nvarchar(8),CURRENT_TIMESTAMP,112) as bigint)
The number of characters in the nvarchar limits the precision. I only need the date.
You cannot convert '2010-11-02 00:00:00'
to 20101102000000
in T-SQL directly so yes, as you say, you would need to modify the string into something you can cast as a bigint
.
declare @str varchar(19) = '2010-11-02 00:00:00'
select cast(replace(replace(replace(@str, '-', ''), ' ', ''), ':', '') as bigint)
Here is a solution:
declare @t datetime = '2010-11-02 00:00:00'
select DATEPART(second, @t) +
DATEPART(minute, @t) * 100 +
DATEPART(hour, @t) * 10000 +
DATEPART(day, @t) * 1000000 +
DATEPART(month, @t) * 100000000 +
DATEPART(year, @t) * 10000000000