Jeremy Poisson
asked on
SQL Sum NVARCHAR(50) Error: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
Good morning team,
I have an error (The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.) when trying to sum hours and minutes (duration column) from a table where the column data type is nvarchar(50). I need to aggregate the hours+minutes so I can join on another table to get total work hours for installers out in the field. The select query below will work for specific CustomerName however, I receive the error attached when running for all customers. I've tried at least 10 different ways to resolve but the error is always the same.
As always, thank you all for your time and expertise in helping me get this resolved!
SELECT TOP (100) PERCENT CustomerName, CONVERT(varchar(10), SUM(DATEDIFF(minute, 0, Duration)) / 60) + '.' + CONVERT(varchar(10), SUM(DATEDIFF(minute, 0,
Duration)) % 60) AS install_hours_worked
FROM dbo.vWIP_Install_TimeSheet s AS T
GROUP BY CustomerName
Here is the source query for the view above. This is simply to refine to a list of time sheet entries that meet certain service item criteria (in this case, we're only looking for a WIP report on installation jobs).
SELECT TOP (100) PERCENT CustomerName, CustomerId, Duration
FROM dbo.TimeTracking
WHERE (CustomerName IN
(SELECT Job
FROM dbo.vWIP_Install)) AND (ServiceItemName IN
(SELECT FullName
FROM dbo.vWip_Items
WHERE (dbo.TimeTracking.Duration > '0.00') AND (CustomFields = 'Install')))
ORDER BY CustomerName
Sum-error-tracking-table-source.png
Sum-error_view.png
I have an error (The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.) when trying to sum hours and minutes (duration column) from a table where the column data type is nvarchar(50). I need to aggregate the hours+minutes so I can join on another table to get total work hours for installers out in the field. The select query below will work for specific CustomerName however, I receive the error attached when running for all customers. I've tried at least 10 different ways to resolve but the error is always the same.
As always, thank you all for your time and expertise in helping me get this resolved!
SELECT TOP (100) PERCENT CustomerName, CONVERT(varchar(10), SUM(DATEDIFF(minute, 0, Duration)) / 60) + '.' + CONVERT(varchar(10), SUM(DATEDIFF(minute, 0,
Duration)) % 60) AS install_hours_worked
FROM dbo.vWIP_Install_TimeSheet
GROUP BY CustomerName
Here is the source query for the view above. This is simply to refine to a list of time sheet entries that meet certain service item criteria (in this case, we're only looking for a WIP report on installation jobs).
SELECT TOP (100) PERCENT CustomerName, CustomerId, Duration
FROM dbo.TimeTracking
WHERE (CustomerName IN
(SELECT Job
FROM dbo.vWIP_Install)) AND (ServiceItemName IN
(SELECT FullName
FROM dbo.vWip_Items
WHERE (dbo.TimeTracking.Duration
ORDER BY CustomerName
Sum-error-tracking-table-source.png
Sum-error_view.png
ASKER
Hi Icohan - I ran the query and have 619 rows out of 137,422 in the TimeTracking table that meet this criteria. How do I omit these so the aggregate can continue?
Assuming they have a row identifier(PK, or some ID unique index) you could put all those ID's in a CTE or a #temp table and then use EXCEPT or NOT EXISTS to exclude those rows from aggregations until data is fixed.
ASKER
Thanks Icohan - can you give me an example of a CTE or temp table query?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you VERY MUCH! The error has subsided and the data is correct.
Awesome job - I appreciate all of the time, effort and expertise; well done.
Jeremy
Awesome job - I appreciate all of the time, effort and expertise; well done.
Jeremy
https://database.guide/isdate-examples-in-sql-server/