473,548 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use of "CASE" in the select statement

Claus Mygind
571 Contributor
The following code works fine
Expand|Select|Wrap|Line Numbers
  1. select
  2.   concat(
  3.     case t1.testColumn
  4.       when 'value1' then t1.column1
  5.       when 'value2' then t1.column2
  6.       when 'value3' then t1.column3
  7.       else t1.column4
  8.     end
  9.   ) as outputColumn
  10.  
My question is this (because I could not get the following code to work), is is possible to construct the case clause where the concat( ) is located inside the case statement, which would allow for multiple columns to be returned with just one case statement.

Here is my code that did not work.
Expand|Select|Wrap|Line Numbers
  1. select
  2.  case t1.testColumn
  3.   when 'value1' 
  4.     then 
  5.      concat(t1.column1) as outputColumn1,
  6.      concat(t1.column2) as outputColumn2,
  7.      concat(t1.column3) as outputColumn3
  8.   when 'value2' 
  9.     then 
  10.      concat(t1.column4) as outputColumn1,
  11.      concat(t1.column5) as outputColumn2,
  12.      concat(t1.column6) as outputColumn3
  13.   else 
  14.      concat(t1.column7) as outputColumn1,
  15.      concat(t1.column8) as outputColumn2,
  16.      concat(t1.column9) as outputColumn3
  17.  end
  18.  
The purpose of my question is to see if it possible to include several columns in one case statement rather than constructing one for each column I wish to output.

The practical application applies to a billing source reference. A job may be billed to the requesting party, the actual client or some 3rd party unique to that job.
Dec 20 '13 #1
5 13380
Luuk
1,047 Recognized Expert Top Contributor
What do you try to accomplig with i.e. line #5
Expand|Select|Wrap|Line Numbers
  1. concat(t1.column1) as outputColumn1,
concat is a function, and will concatenat all parameters it receives....
But you only give 1 parameter?

Returning multiple columns in a CASE WHEN is not possible

Maybe you can do:
Expand|Select|Wrap|Line Numbers
  1. select t1.column1, t1.column2,t1.column3
  2. where t1.tesColumns='value1'
  3. union
  4. select t1.column4, t1.column5,t1.column6
  5. where t1.tesColumns='value2'
  6. union
  7. select t1.column7, t1.column8,t1.column9
  8. where t1.tesColumns='value3'
  9.  
Dec 20 '13 #2
zmbd
5,501 Recognized Expert Moderator Expert
If not the union then perhaps:
remove the CONCAT() and break the three sections you have down to individuals:

so for, an example using just the first section:
Expand|Select|Wrap|Line Numbers
  1. select 
  2.    case t1.testColumn 
  3.       when 'value1' then 
  4.          t1.column1 as outputColumn1, 
  5.    case t1.testColumn
  6.       when 'value1' then
  7.          t1.column2 as outputColumn2, 
  8.    case t1.testColumn
  9.       when 'value1' then
  10.          t1.column3 as outputColumn3
  11. (... repeat for each of your
  12. sub sections... yes it's 9 lines ...)
There is the case..when..the n..else construct; however, I don't see how to push that forward for what you have.

Just to clarify what Luuk is saying about Concat()
SYNTAX: Concat(str1,str 2[,str3,][...])
resolves as "str1str2str3.. ."
thus if str1 = "a", str2 = "b", and str3 = "c"
then the return is "abc"
Dec 20 '13 #3
Claus Mygind
571 Contributor
I think Luuk has summed it up "Returning multiple columns in a CASE WHEN is not possible"

Perhaps it has to be done with sub-queries.

Each record contains 3 sets of Company information (name, address, city, state, zip code etc. etc.)

1. Requesting party
2. Actual client
3. Where to send the bill

For the form I am developing, I want to select one SET of company information. So I was trying to return multiple columns in one case statement, instead of creating multiple case statements.

I can simply return all 3 company sets of information and then select the piece of the data I want to use with my php code. I was simply experimenting with developing a better sql query that would require less coding downstream.

Thank you both for you suggested help.
Dec 20 '13 #4
Luuk
1,047 Recognized Expert Top Contributor
Your original database design is wrong...

if you have a table with 3 sets of company info, you should change that. One record should only containt 1 set of company info

if you have (simplified):
Expand|Select|Wrap|Line Numbers
  1. select 
  2. name1, address1,
  3. name2, address2,
  4. name3, address3
  5. from mytable
you should change this to:
Expand|Select|Wrap|Line Numbers
  1. select
  2. 1 as id, name1, address1
  3. from mytable
  4. union
  5. select
  6. 2 as id, name2, address2
  7. from mytable
  8. union
  9. select
  10. 3 as id,name3, address3
  11. from mytable
this way you can see all your requesting party's
Expand|Select|Wrap|Line Numbers
  1. select * from view where id=1
or all your actual clients
Expand|Select|Wrap|Line Numbers
  1. select * from view where id=2
check i.e. how to create a view
Dec 20 '13 #5
Claus Mygind
571 Contributor
Yes I am familiar with normalizing data in relational data base system and that structure has been implemented. That was not really the question for this exercise.

But I thank you for your input.
Dec 23 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
3499
by: Guy Hocking | last post by:
Hi there, I have a problem in my ASP/SQL Server application i am developing, i hope you guys can help. I have a ASP form with list boxes populated by SQL tables. When a user selects a value in a list box and submits the form the value is put into a session variable and the relevant page is displayed (in accordance to one of the list...
1
4467
by: brett | last post by:
Here is my SQL string: "SELECT to_ordnum, to_orddate," _ & "(SELECT SUM((DDPROD.pr_stanmat * DDPROD.pr_prfact) * (DOBOM2.b2_quant * DDORD.or_quant)) FROM DDPROD INNER JOIN DOBOM2 ON DDPROD.pr_prodnum = DOBOM2.b2_prodnum INNER JOIN DDORD ON DOBOM2.b2_orid = DDORD.or_id INNER JOIN DDTORD ON DDORD.OR_TOID = DDTORD.TO_ID WHERE DOBOM2.b2_ordnum...
1
11319
by: John Hall | last post by:
We need to read a SQL database containing a mix of English words and Chinese Characters. We think we need to use the N'xxxx' to read the Unicode. We have one place where the SELECT statement works fine. Here is that statement: dim objCmdCategories as new SQLDataAdapter ("select distinct N'product_category' from " & _ "tblproducts where...
2
1575
by: GIS Analyst | last post by:
Hi to all I wish to be able to have a standard select statement which has additional fields added to it at run-time based on supplied parameter(s). ie declare @theTest1 nvarchar(10) set @theTest1='TRUE'
15
2719
by: grunar | last post by:
After some thought on what I need in a Python ORM (multiple primary keys, complex joins, case statements etc.), and after having built these libraries for other un-named languages, I decided to start at the bottom. What seems to plague many ORM systems is the syntactic confusion and string-manipulation required to build the SQL Statements. If...
2
1308
by: patang | last post by:
I am displaying the records from a table in a grid. The simpel SELECT statement works fine. However, I have to use this select statement select right("00000" + Column1,2), column2, column3 FROM mytable This does not work. I mean it is not showing the 0000's in the first column. The above statement is fine if I run it in SQL Server but how...
3
1716
by: sangam56 | last post by:
Hello!I am using following sql statement: SELECT Menu.MenuID,Menu.TextUrl FROM Menu WHERE Menu.MenuID= (SELECT Permissions.MenuID FROM Permissions WHERE Permissions.RoleID=(SELECT Roles.RoleID FROM Roles WHERE Roles.RoleName=@inputRoleName)) I am using three tables: Roles, Permissions, and Menu. The error reads like: use of =,<,> etc cannot...
2
4818
by: ankitmathur | last post by:
Hi All, I'm facing a problem I'm unable to find a solution for. I hope fellow members would be able to help me out. System Info: PHP v5 MSSQL 2008 Linux box
1
3676
by: ncsthbell | last post by:
Once again... I am supporting another Access database that I did not build. The previous 'owner' of this built the tables with spaces in the table names as well as the column names. I am having a difficult time getting the select statement to work. I am getting a run-time error 2342. "A RunSql action requires and arguement consisting fo an SQL...
2
1449
by: thewebdude | last post by:
Hi, I am doing a project with dreamweaver and i ran into a hitch with my select statement. I have this SELECT * FROM client, `statement` WHERE client.client_id = `statement`.client_id when i execute this it selects everybody in the database. So i want to add another WHERE statement to this query, this time one that compares...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.