June 11, 2004 at 1:10 pm
What value of 'Result' will be printed below? (It wasn't what I expected...)
DECLARE @F FLOAT
SELECT @F = 1234.5678
SELECT CASE
WHEN @F < 1.0 THEN CONVERT(DECIMAL(10, 4), @F)
ELSE CONVERT(DECIMAL(10, 0), @F)
END AS 'Result'
June 11, 2004 at 1:58 pm
-- This is OK (What I Expect) -- Result: 1234.5678
DECLARE @F1 FLOAT
SELECT @F1 = 1234.5678
SELECT CONVERT(DECIMAL(10, 4), @F1)
-- This is OK (What I Expect) -- Result: 1235
DECLARE @F2 FLOAT
SELECT @F2 = 1234.5678
SELECT CONVERT(DECIMAL(10, 0), @F2)
-- Huh? (I Don't Understand This At All) -- Result: 1235.0000
DECLARE @F3 FLOAT
SELECT @F3 = 1234.5678
SELECT CASE
WHEN @F3 < 1.0 THEN CONVERT(DECIMAL(10, 4), @F3)
ELSE CONVERT(DECIMAL(10, 0), @F3)
END AS 'Result'
June 11, 2004 at 11:30 pm
@F3 was greater than one so the ELSE prevails giving you the exact same answer as you had in your second example.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 13, 2004 at 3:09 pm
From BOL:
The return type from a CASE statement is:
"Returns the highest precedence type from the set of types in result_expressions and the optional else_result_expression"
So You get DECIMAL(10, 4) and not DECIMAL(10, 0) since it has higher precedence
/rockmoose
You must unlearn what You have learnt
June 13, 2004 at 3:21 pm
Dummy me... missed the formatting thing... thought he didn't understand the "ELSE". Nice job, Rockmoose!
--Jeff Moden
Change is inevitable... Change for the better is not.
June 13, 2004 at 3:28 pm
Yeah, I assumed it just wasn't the ELSE thing.
Too simple, it must have been something else that upset John Jakob !
cheers,
/rockmoose
You must unlearn what You have learnt
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply