Convert Datetime to Bigint (or string) and Change back to Datetime


  

 declare @dtBigInt bigint
--declare @dtstringFromDatetime varchar(14)

select @dtBigInt=CAST(concat(year(getdate())
,Right('0'+Cast(month(getdate()) as varchar(2)),2)
,Right('0'+Cast(day(getdate()) as varchar(2)),2)
,Right('0'+Cast(datepart(hour, getdate()) as varchar(2)),2)
,Right('0'+Cast(datepart(minute,getdate()) as varchar(2)),2)
,Right('0'+Cast(datepart(second,getdate()) as varchar(2)),2)
) as BIGINT)
print @dtBigInt

--declare @dtstringFromDatetime varchar(20)
declare @dt datetime
--Convert bigint to datetime
select @dt=cast(stuff(stuff(stuff (Cast(@dtBigInt as varchar(20)), 9,0,' '),12,0,':'),15,0,':') as datetime)
print @dt
--STUFF function with length 0 will insert string to the string after the starting position


 

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/ca6426f6-add6-47d7-a110-9620d9864c9a/convert-string-to-datetime?forum=transactsql

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/6dc38e9e-70b2-42d2-bbfd-2fbae0791ea8/sql-2012-convert-datetime-to-interget-value?forum=transactsql

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s