November 16, 2023 at 2:53 pm
Have a numeric column HRS Numeric(5,2)
Need to add error if the value after decimal point second digit is not 0
Example - 50.10 is acceptible
50.77 is not acceptable (Second digit should be Zero after decimal point)
November 16, 2023 at 3:03 pm
;WITH cte_data AS (
SELECT CAST(50.10 AS numeric(5, 2)) AS HRS
UNION ALL
SELECT 50.77
)
SELECT HRS
FROM cte_data
WHERE HRS LIKE '%.[0-9][^0]%' --<<--
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
November 16, 2023 at 3:04 pm
(Dup post, removed)
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
November 16, 2023 at 5:40 pm
You could test if the modulo .1 of HRS equals zero
;WITH cte_data AS (
SELECT CAST(50.10 AS numeric(5, 2)) AS HRS
UNION ALL
SELECT 50.77
)
SELECT HRS
FROM cte_data
WHERE HRS%.1=0;
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
November 16, 2023 at 5:47 pm
Have a numeric column HRS Numeric(5,2)
Need to add error if the value after decimal point second digit is not 0
Example - 50.10 is acceptible
50.77 is not acceptable (Second digit should be Zero after decimal point)
This is a strange requirement. I'm curious what the use case is.
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
November 18, 2023 at 9:05 am
To check the second digit after the decimal, try this:
sqlCopy code
IF (HRS * 100) % 10 <> 0 THEN
-- Add your error handling here
-- This means the second digit after the decimal is not zero
END IF;
This code multiplies the number by 100 and checks if the second digit after the decimal isn't zero. If it's not zero, an error can be handled as needed.
November 18, 2023 at 9:31 pm
Nearly duplicate post with an incorrect improvement calculation removed.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 18, 2023 at 9:32 pm
You could test if the modulo .1 of HRS equals zero
;WITH cte_data AS (
SELECT CAST(50.10 AS numeric(5, 2)) AS HRS
UNION ALL
SELECT 50.77
)
SELECT HRS
FROM cte_data
WHERE HRS%.1=0;
Being in love with the terms "SQL Death by a Million Cuts" and "Milliseconds Matter", your method tests out at being 31% faster than the "LIKE" method previously used.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 19, 2023 at 1:03 pm
Thank you Jeff. 44.9% or 31%? Either way there's no implicit type conversion 🙂
Ken McKelvey's solution in the "Pivot with grouping intervals" topic is interesting. It cross join's a 2 row virtual table { (1), (-1) } and then (in a single start/end column) uses cross apply to make the 1 row the start date and the -1 row the end date. Then the query orders and aggregates across the start/end column. It's not so intuitive yet tho. My current method (as seen on SSC dozens of times) of dealing with overlapping timespans expands the intervals and looks for gaps using lead/lag. In my head I have a generic scenario that goes along with it. For Ken's method it's basically an unpivoting of the intervals
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
November 19, 2023 at 9:00 pm
Thank you Jeff. 44.9% or 31%? Either way there's no implicit type conversion 🙂
Ken McKelvey's solution in the "Pivot with grouping intervals" topic is interesting. It cross join's a 2 row virtual table { (1), (-1) } and then (in a single start/end column) uses cross apply to make the 1 row the start date and the -1 row the end date. Then the query orders and aggregates across the start/end column. It's not so intuitive yet tho. My current method (as seen on SSC dozens of times) of dealing with overlapping timespans expands the intervals and looks for gaps using lead/lag. In my head I have a generic scenario that goes along with it. For Ken's method it's basically an unpivoting of the intervals
It's 31% savings. I did the original calculation "upside down" (I originally did Old/New-1 instead of the correct New/Old-1) . Here are the run results from the previous posted test code.
--===== Using the Modulo Method
SQL Server Execution Times:
CPU time = 2296 ms, elapsed time = 2323 ms.
--===== Using the LIKE method
SQL Server Execution Times:
CPU time = 3375 ms, elapsed time = 3367 ms.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply