sql - Convert String to Datetime Error -


i writing query poll 1 of our devices , report status our solarwinds server. semi-advanced sql query, results come out correct error message:

msg 242, level 16, state 3, line 1 conversion of nvarchar data type datetime data type resulted in out-of-range value.

i new sql i'm not sure resolve. code block below:

select tbl2.load [source], tbl3.destination [destination], tbl1.status [status], tbl4.status [timestamp], convert(datetime, substring (tbl4.status,5,3) + ' ' + substring(tbl4.status,9,2) + ' , ' +  substring(tbl4.status,25,4) + ' ' + substring(tbl4.status,11,6)) date,  datediff(hour,convert(datetime, substring (tbl4.status,5,3) + ' ' + substring(tbl4.status,9,2) + ' , ' +  substring(tbl4.status,25,4) + ' ' + substring(tbl4.status,11,6)) , getdate()) timediff nodes n   join (select ca.nodeid, cs.status [load]      custompollerstatus cs  join custompollerassignment ca on (cs.custompollerassignmentid=ca.custompollerassignmentid) join custompollers cp on ca.custompollerid=cp.custompollerid      cp.uniquename='snapmirrorsrc'      ) tbl2 on n.nodeid =  tbl2.nodeid  join (select ca.nodeid, cs.status [destination]     custompollerstatus cs  join custompollerassignment ca on (cs.custompollerassignmentid=ca.custompollerassignmentid) join custompollers cp on ca.custompollerid=cp.custompollerid cp.uniquename='snapmirrordst' ) tbl3 on n.nodeid = tbl3.nodeid  join (select ca.nodeid, cs.status      custompollerstatus cs  join custompollerassignment ca on (cs.custompollerassignmentid=ca.custompollerassignmentid)      join custompollers cp on ca.custompollerid=cp.custompollerid      cp.uniquename='snapmirrorstate'      ) tbl1 on n.nodeid = tbl1.nodeid  join (select ca.nodeid, cs.status custompollerstatus cs  join custompollerassignment ca on (cs.custompollerassignmentid=ca.custompollerassignmentid)  join custompollers cp on ca.custompollerid=cp.custompollerid cp.uniquename='snapmirrormirrortimestamp' ) tbl4 on n.nodeid = tbl4.nodeid  tbl1.status 'unk%'  

here results:

enter image description here

however error: msg 242, level 16, state 3, line 1 conversion of nvarchar data type datetime data type resulted in out-of-range value.

so results come out correct, im getting error. not sure issue is. kindly appreciated.

****--- edit ---****

so changed code bit,however, in date column im getting 1900-01-01 00:00:00.000

select tbl2.load [source], tbl3.destination [destination], --tbl1.status    [status],  tbl4.status [timestamp],  case  when tbl4.status '[a-z][a-z][a-z] %[1-9][1-9][1-9][1-9]'  convert(datetime, substring (tbl4.status,5,3) + ' ' +    substring(tbl4.status,9,2) + ' , ' +  substring(tbl4.status,25,4) + ' ' +    substring(tbl4.status,11,6))  else '' end [date],   case  when tbl4.status '[a-z][a-z][a-z] %[1-9][1-9][1-9][1-9]'  cast (datediff(hour,convert(datetime, substring (tbl4.status,5,3) + ' ' +     substring(tbl4.status,9,2) + ' , ' +  substring(tbl4.status,25,4) + ' ' +     substring(tbl4.status,11,6)) , getdate())as bigint) else '' end 'time since last snap' nodes n   join (select ca.nodeid, cs.status [load]    custompollerstatus cs    join custompollerassignment ca on     (cs.custompollerassignmentid=ca.custompollerassignmentid)    join custompollers cp on ca.custompollerid=cp.custompollerid    cp.uniquename='snapmirrorsrc'    ) tbl2 on n.nodeid =  tbl2.nodeid  join (select ca.nodeid, cs.status [destination] custompollerstatus cs  join custompollerassignment ca on    (cs.custompollerassignmentid=ca.custompollerassignmentid) join custompollers cp on ca.custompollerid=cp.custompollerid cp.uniquename='snapmirrordst' ) tbl3 on n.nodeid = tbl3.nodeid  join (select ca.nodeid, cs.status custompollerstatus cs  join custompollerassignment ca on     (cs.custompollerassignmentid=ca.custompollerassignmentid)  join custompollers cp on ca.custompollerid=cp.custompollerid cp.uniquename='snapmirrormirrortimestamp' ) tbl4 on n.nodeid = tbl4.nodeid 

there perhaps error retrieving year part: try substring(tbl4.status,22,4) instead of substring(tbl4.status,25,4)


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -