I am writing a query to poll one of our devices and report back to the status to our Solarwinds server. It is a semi-advanced SQL Query, my results come out correct but I get this error message:
Msg 242, Level 16, State 3, Line 1 The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
I am new to SQL so I'm not sure what to do to resolve. My code block is 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)) AS 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()) AS Timediff
FROM Nodes n
join (SELECT ca.nodeid, cs.Status [Load]
FROM CustomPollerStatus cs
JOIN CustomPollerAssignment ca ON (cs.CustomPollerAssignmentID=ca.CustomPollerAssignmentID)
JOIN CustomPollerS cp ON ca.CustomPollerID=cp.CustomPollerID
where cp.UniqueName='SnapMirrorSrc'
) tbl2 on n.Nodeid =
tbl2.NodeID
join (select ca.nodeid, cs.Status [Destination]
FROM CustomPollerStatus cs
JOIN CustomPollerAssignment ca ON (cs.CustomPollerAssignmentID=ca.CustomPollerAssignmentID)
JOIN CustomPollerS cp ON ca.CustomPollerID=cp.CustomPollerID
where cp.UniqueName='snapmirrordst'
) tbl3 on n.Nodeid = tbl3.NodeID
join (SELECT ca.nodeid, cs.Status
FROM CustomPollerStatus cs
JOIN CustomPollerAssignment ca ON (cs.CustomPollerAssignmentID=ca.CustomPollerAssignmentID)
JOIN CustomPollerS cp ON ca.CustomPollerID=cp.CustomPollerID
WHERE cp.UniqueName='SnapMirrorState'
) tbl1 on n.Nodeid = tbl1.NodeID
join (select ca.nodeid, cs.Status
from CustomPollerStatus cs
join CustomPollerAssignment ca ON (cs.CustomPollerAssignmentID=ca.CustomPollerAssignmentID)
JOIN customPollerS cp on ca.CustomPollerID=cp.CustomPollerID
WHERE cp.UniqueName='snapmirrorMirrorTimestamp'
) tbl4 on n.NodeID = tbl4.NodeID
where tbl1.Status like 'unk%'
Here are my results:
However I get this error: Msg 242, Level 16, State 3, Line 1 The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
So my results come out correct, but Im getting this error. Not sure what the issue is. Any help would be kindly appreciated.
****--- EDIT ---****
So I changed my code a bit,however, now in the 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 LIKE '[A-Z][A-Z][A-Z] %[1-9][1-9][1-9][1-9]' THEN
CONVERT(datetime, SUBSTRING (tbl4.Status,5,3) + ' ' + SUBSTRING(tbl4.Status,9,2) + ' , ' + SUBSTRING(tbl4.Status,25,4) + ' ' + SUBSTRING(tbl4.Status,11,6))
ELSE '' END AS [DATE],
CASE
WHEN tbl4.status LIKE '[A-Z][A-Z][A-Z] %[1-9][1-9][1-9][1-9]' THEN
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 AS 'Time Since Last Snap'
FROM Nodes n
join (SELECT ca.nodeid, cs.Status [Load]
FROM CustomPollerStatus cs
JOIN CustomPollerAssignment ca ON (cs.CustomPollerAssignmentID=ca.CustomPollerAssignmentID)
JOIN CustomPollerS cp ON ca.CustomPollerID=cp.CustomPollerID
where cp.UniqueName='SnapMirrorSrc'
) tbl2 on n.Nodeid =
tbl2.NodeID
join (select ca.nodeid, cs.Status [Destination]
FROM CustomPollerStatus cs
JOIN CustomPollerAssignment ca ON (cs.CustomPollerAssignmentID=ca.CustomPollerAssignmentID)
JOIN CustomPollerS cp ON ca.CustomPollerID=cp.CustomPollerID
where cp.UniqueName='snapmirrordst'
) tbl3 on n.Nodeid = tbl3.NodeID
join (select ca.nodeid, cs.Status
from CustomPollerStatus cs
join CustomPollerAssignment ca ON (cs.CustomPollerAssignmentID=ca.CustomPollerAssignmentID)
JOIN customPollerS cp on ca.CustomPollerID=cp.CustomPollerID
WHERE cp.UniqueName='snapmirrorMirrorTimestamp'
) tbl4 on n.NodeID = tbl4.NodeID