Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select query

Author  Topic 

gagani
Posting Yak Master

112 Posts

Posted - 2012-06-28 : 05:33:01
customerid forename1 surname1 forename2 surname2
101 gordon carolyn tattersall

if there is nothing in surname1, then i should get the
output as surname2

from the above table, i should get the output as

tattersall

aakcse
Aged Yak Warrior

570 Posts

Posted - 2012-06-28 : 05:36:34
try using case statement to get the result

-Neil
Go to Top of Page

gagani
Posting Yak Master

112 Posts

Posted - 2012-06-28 : 05:40:19
I am unable to get it right with the case. could you please write the query.

many thanks
Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-06-28 : 08:51:09
quote:
Originally posted by gagani

customerid forename1 surname1 forename2 surname2
101 gordon carolyn tattersall

if there is nothing in surname1, then i should get the
output as surname2

from the above table, i should get the output as

tattersall



can you explain more please?

--------------------------
Get rich or die trying
--------------------------
Go to Top of Page

anchoredwisdom
Starting Member

22 Posts

Posted - 2012-06-29 : 09:44:49
SELECT CUSTOMERID,FORENAME1,SURNAME`,FORENAME2, DECODE(SURNAME2,NULL,SURNAME1,SURNAME2 END) SURNAME2 FROM TABLE1
Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-06-29 : 10:04:10
quote:
Originally posted by anchoredwisdom

SELECT CUSTOMERID,FORENAME1,SURNAME`,FORENAME2, DECODE(SURNAME2,NULL,SURNAME1,SURNAME2 END) SURNAME2 FROM TABLE1



I think your select is an Oracle query!!


if so, Please try NVL or NVL2 functions.

if you are using SQL Server try this:
SELECT CUSTOMERID,FORENAME1,SURNAME,FORENAME2, case
when SURNAME1 is null then SURNAME2
else SURNAME1
end as name_column
FROM TABLE1


Remember that this is a SQL server Forum.

Luck

--------------------------
Get rich or die trying
--------------------------
Go to Top of Page
   

- Advertisement -