School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,212 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,561 people online right now. Registration is fast and FREE... Join Now!




Can I use CASE to determine which table columns to select?

 

Can I use CASE to determine which table columns to select?

BPAWthe1st

19 Jun, 2009 - 11:43 AM
Post #1

New D.I.C Head
*

Joined: 19 Jun, 2009
Posts: 2

I'm trying to create a query where if the first field is null, it will go to another fields. I tried something like this:

SELECT table1.column1, table1.column2, table2.column1, table2.column2
CASE
WHEN table3.column1 IS NULL THEN table4.column1
ELSE table3.column1
END
FROM table1, table2, table3, table4

But it was not working. Is case not the right thing to use here or is my formatting off? Any help would be appreciated.

User is offlineProfile CardPM
+Quote Post


June7

RE: Can I Use CASE To Determine Which Table Columns To Select?

19 Jun, 2009 - 02:43 PM
Post #2

D.I.C Regular
Group Icon

Joined: 9 Dec, 2008
Posts: 485



Thanked: 38 times
My Contributions
What database app? Where is this statement - code, form, report, saved query? If code could select all the fields then use nested in-line if to set a variable? I have seen case structure in a query but never built one. However, you are missing JOIN or UNION clause for the tables.

If this is Access can use Nz funtion and maybe (test it in query designer):
CODE
SELECT Nz(table1.column1,Nz(table1.column2,Nz(table2.column1,Nz(table2.column2,table3.column1)))) As MyData FROM ... JOIN ... WHERE ... ORDER BY ...


This post has been edited by June7: 20 Jun, 2009 - 09:35 AM
User is offlineProfile CardPM
+Quote Post

Robin Marsh

RE: Can I Use CASE To Determine Which Table Columns To Select?

20 Jun, 2009 - 05:07 AM
Post #3

New D.I.C Head
*

Joined: 7 May, 2009
Posts: 13



Thanked: 1 times
My Contributions
A SQL Server query could be as follows...

CODE

SELECT
     tbl1.[field1],
     tbl2.[field2],
     Case When tbl1.[field1] Is Not Null Then tbl1.[field1] Else tbl2.[field1] End
FROM Table1 tbl1
LEFT JOIN Table2 tbl2 On tbl2.[id] = tbl1.[id]
WHERE tbl1.[id] > 0


User is offlineProfile CardPM
+Quote Post

BPAWthe1st

RE: Can I Use CASE To Determine Which Table Columns To Select?

22 Jun, 2009 - 09:38 AM
Post #4

New D.I.C Head
*

Joined: 19 Jun, 2009
Posts: 2

It is in a query and there is a WHERE clause but that doens't impact the selection. I tried to second post's code and it gave me the same error as when I tried it, saying "missing keyword." I'm using Oracle SQL Developer to eventually be placed in Oracle Reports Builder.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 09:46PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month