sql server - SQL Date-Time Conversion with nested converts experiences random conversion errors -
looking assistance strange issue if has ideas:
i have sql statement works of time in t-sql script crashes occasionally. have identified data crash occurs on , cannot identify difference between data rows work.
the goal of code add time existing datetime value has 00:00:00 time second time column (as outlined below). goal combine both columns yyyy-mm-dd hh:mm:ss format, had convert them char first trim off orignal 00:00:00.
columns
logdate - contains date in datetime format (yyyy-mm-dd hh:mm:ss)
logtime - contains time of action , in varchar format (hh:mm)
sql conversion
select convert(datetime, convert(char(8), logdate, 112) + ' ' + convert(char(8), logtime, 108)) testtable eventserial = '100001' however, if change eventserial in above statement different row, such '100002', statement works.
the data each row below:
eventserial 100001's values: logdate: 2015-04-02 00:00:00.000 logtime: 10:04
eventserial 100002's values: logdate: 2015-04-02 00:00:00.000 logtime: 10:48
running data set 1 fails, running data set 2 produces output. also, running code without final datetime conversion works, or if run code string manually works (as outlined below:)
select convert(char(8), logdate, 112) + ' ' + convert(char(8), logtime, 108) testtable eventserial = '100001' select convert(datetime, '20150402 10:48') select convert(datetime, '20150402 10:04') any suggestions, i'm sure silly i'm missing (and took long way around issue anyway. desired output 2015-04-02 10:04:00
first, datetime has no format. (why?)
second, don't need convert datetime value char add hours , minutes, use dateadd:
select dateadd(minute, cast(right(logtime, 2) int), dateadd(hour, cast(left(logtime, 2) int), logdate ) ) testtable eventserial = '100001' also, note convert not hold style yyyymmdd hh:mm
note: code written directly here, there might mistakes.
Comments
Post a Comment