sql server - SQL Most efficient way to create int value from dateTime -


using sql server 2008 r2, have trigger updates field after update. inserts date each of updated rows. line in trigger action:

set t.lastupdateddatetime = current_timestamp 

using yyyymmddhhmmss above line insert 2015-07-16 16:19:00. have situation business wants integer version of this.

what effective way achieve this. have work seems long , expensive:

set t.lastupdateddatetimeint = cast('' + cast(year(getdate()) varchar(4)) + right('0' + cast((month(getdate())) varchar(2)), 2) + right('0' + cast((day(getdate())) varchar(2)), 2) + right('0' + cast((datepart(hh, getdate())) varchar(2)), 2) + right('0' + cast((datepart(mi, getdate())) varchar(2)), 2) + right('0' + cast((datepart(ss, getdate())) varchar(2)), 2)  bigint) 

is there better more efficient way this? desired outcome 20150716161900

a secondary question. not fan of storing dates integers @ all. in fact have never done before current role. can ask opinions on this, great reasons avoid this? there few reasons why there preference in organisation. here one, every time client hit webservice datetime value delivered content, when client hits webservice in future need pass in datetime value effectivley best data back. simply, business afraid come in different format.

edited:

here full trigger completness:

    alter trigger [dbo].[trig_lastupdated]     on [dbo].[aadeleteme]      after update         begin          if not update(lastupdateddatetime)         begin             update t                  -- original code (the long way result)                  --set t.lastupdateddatetimeint = cast('' + cast(year(getdate()) varchar(4)) + right('0' + cast((month(getdate())) varchar(2)), 2) + right('0' + cast((day(getdate())) varchar(2)), 2) + right('0' + cast((datepart(hh, getdate())) varchar(2)), 2) + right('0' + cast((datepart(mi, getdate())) varchar(2)), 2) + right('0' + cast((datepart(ss, getdate())) varchar(2)), 2)  bigint)                  --and here solution below (the shortway result)                  --set t.lastupdateddatetimeint = replace(replace((convert(varchar(10),current_timestamp,111) + convert(varchar(8),current_timestamp,114)),'/',''),':','')                 --just discovered 112 format managed drop 1 replace                 set t.lastupdateddatetimeint = replace((convert(varchar(10),current_timestamp,112) + convert(varchar(8),current_timestamp,114)),':','')                  dbo.aadeleteme t                  inner join inserted                  on t.id = i.id;         end     end      go 

if want integer part of date, 1 of solution remove separators in date value ex:- '/', '-', ':' , ' ' space date, , date format want:-

select replace(replace(replace ( '2015-07-16 16:19:00' , '-' , '' ),':',''),' ','') 

output:- 20150716161900

see demo

update:-

use per requirement

select replace(replace((convert(varchar(10),current_timestamp,101)                  + convert(varchar(8),current_timestamp,114)),'/',''),':','') 

and if column lastupdateddatetimeint int going throw overflow error, overcome have change datatype column value going assign quite big int


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -