sql - Adding time and date -


i have 2 columns in mssql 2008 r2: workingdate , startime. due mistake in design of application starttime not defined date+time, time (e.g. 1899-12-30 16:00:00.000 4:00 pm)

now, facing problem cannot add date , time.

why

 select convert(datetime,'2015-07-01 00:00:00.000') + convert(datetime,'1899-12-30 16:00:00.000')  2015-06-29 16:00:00.000  , not 2015-07-01 16:00:00.000 

thanks help

sorry: found solution: date comes vba , first day 1899-12-30 , not 1900-01-01 in sql !!! therefore have add +2 !!

try this:

select dateadd(second,                  datepart(hour,'1899-12-30 16:00:00.000') * 3600 +                  datepart(minute,'1899-12-30 16:00:00.000') * 60 +                  datepart(second,'1899-12-30 16:00:00.000'),                 '2015-07-01 00:00:00.000') 

the idea use dateadd function add needed time in seconds base date (2015-07-01 00:00:00.000). in order convert record time seconds using datepart function 3 time extract hours, minutes , seconds record date.

to fix date in table can use this:

update [dbo].[mytable] set [date] =  dateadd(second, datepart(hour, [date]) * 3600 + datepart(minute, [date]) * 60 + datepart(second, [date], '2015-07-01 00:00:00.000'); 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -