Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

CASE condition not affecting CAST output Expand / Collapse
Author
Message
Posted Tuesday, June 10, 2014 8:40 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, July 17, 2014 12:09 AM
Points: 331, Visits: 240
Hi,

I've a variable in my SP that I'd like to use to change the format of my output. Try this!

select case when 1=2 then cast(1 as decimal(10,2)) else cast(2 as decimal(10,0)) end

Then this:

select case when 1=1 then cast(1 as decimal(10,2)) else cast(2 as decimal(10,0)) end

Can anyone help me understand (and address) why in either case, the output is returned as n.nn ? Whilst single select expressions (like these):

select cast(1 as decimal(10,2)) -- = 1.00
select cast(2 as decimal(10,0)) -- = 2

Work fine. Ah well. Perhaps there is another way...

Regards Greg.

Post #1579240
Posted Tuesday, June 10, 2014 8:48 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:47 AM
Points: 5,229, Visits: 9,448
Greg

The CASE expression returns a result with the highest precedence data type of the all the result expressions. I'm not sure how you get round that.

John
Post #1579245
Posted Tuesday, June 10, 2014 8:50 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:01 PM
Points: 3,333, Visits: 7,192
A CASE statement can only return one type of data. All possible returned values should be the same. Imagine that you have something more extreme such as
select case when datatype='int' then cast(20140610 as int) 
when datatype='char' then cast(20140610 as char(8))
when datatype='date' then cast(20140610 as date) end
FROM (SELECT 'int' AS datatype, '20140610' AS value UNION ALL
SELECT 'char' AS datatype, '20140610' AS value UNION ALL
SELECT 'date' AS datatype, '20140610' AS value)x

You can't have a column with several data types. Each column must have a single data type.
Why would you want to go against basic database theory?
For format issues, you should handle them in the front end.



Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1579248
Posted Tuesday, June 10, 2014 9:17 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, July 17, 2014 12:09 AM
Points: 331, Visits: 240
Thanks Chaps,

The data is ultimately passed back to the front end as a string, it'll look like this, either

10:5:2

or

12.50:16.50:10.25

I need to format each number before joining them in a string. Each string goes into a datagrid cell. There's no way to format or change this in the front end, hence the need to sort it out in SQL beforehand.

There is a way to do it, *I think*, but it's cumbersome. It'd mean having 2 blocks of code triggered by my variable that controls output format. Anyone have a better idea?

Regards, Greg.
Post #1579263
Posted Tuesday, June 10, 2014 9:37 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:01 PM
Points: 3,333, Visits: 7,192
Or you could use a single data type such as a (var)char.

SELECT CASE WHEN decimals = 0 
THEN LEFT( CAST( CAST( number AS decimal(10,2)) AS varchar(11)), LEN(CAST( CAST( number AS decimal(10,2)) AS varchar(11))) - 3)
ELSE CAST( CAST( number AS decimal(10,2)) AS varchar(11)) END
FROM (VALUES(10, 0),(5, 0),(2, 0),(12.50, 2),(16.50, 2),(10.25, 2))x(number,decimals)




Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1579276
Posted Tuesday, June 10, 2014 9:45 AM This worked for the OP Answer marked as solution
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, July 17, 2014 12:09 AM
Points: 331, Visits: 240
Thanks for this. The query is quite simple, so I'm going to do it like this:


declare @route int
set @route = 2

if @route = 1
begin
select cast(1 as decimal(10,2)) as greg
end

if @route = 2
begin
select cast(1 as decimal(10,0)) as greg
end

Post #1579279
Posted Tuesday, June 10, 2014 10:02 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:49 PM
Points: 13,069, Visits: 11,908
greg.bull (6/10/2014)
Thanks for this. The query is quite simple, so I'm going to do it like this:


declare @route int
set @route = 2

if @route = 1
begin
select cast(1 as decimal(10,2)) as greg
end

if @route = 2
begin
select cast(1 as decimal(10,0)) as greg
end



You might want to read this article about multiple execution paths. It can greatly affect performance unless you handle it correctly.

http://sqlinthewild.co.za/index.php/2009/09/15/multiple-execution-paths/


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1579293
Posted Tuesday, June 10, 2014 10:11 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, July 17, 2014 12:09 AM
Points: 331, Visits: 240
Thanks Sean, will do.
Post #1579303
Posted Wednesday, June 11, 2014 12:13 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, July 20, 2014 11:55 AM
Points: 1,945, Visits: 2,860
>> I've a variable in my SP that I'd like to use to change the format of my output. <<

This is called “flag coupling” in Software Engineering. It is considered awful programming in any language, not just SQL. Your module changes from one environment to another.

CASE WHEN 1=2 THEN CAST(1 AS DECIMAL(10,2)) ELSE CAST(2 AS DECIMAL(10,0)) END

Your code is getting worse. You have no idea how a CASE expression works. We look at the THEN and ELSE clauses to determine the data type of the expression as a whole! It is the “highest data type”, which means this is DECIMAL(10,2) and the WHEN clause have nothing to do with it.

SQL is a strongly typed language; why would anyone think they can change types at execution time?


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1579774
Posted Saturday, June 14, 2014 6:29 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 11:38 AM
Points: 1,780, Visits: 5,655
It's a bit strange to see people boldly stating that you cannot do this, when you can, you just need to cast them as SQL_VARIANT.

I'm not saying you should do this....






MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw



  •   Post Attachments 
    sql_variant.PNG (43 views, 174.73 KB)
    Post #1580904
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse