SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

gagani
Yak Posting Veteran

84 Posts

Posted - 06/28/2012 :  05:33:01  Show Profile  Reply with Quote
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

India
517 Posts

Posted - 06/28/2012 :  05:36:34  Show Profile  Reply with Quote
try using case statement to get the result

-Neil
Go to Top of Page

gagani
Yak Posting Veteran

84 Posts

Posted - 06/28/2012 :  05:40:19  Show Profile  Reply with Quote
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

USA
261 Posts

Posted - 06/28/2012 :  08:51:09  Show Profile  Reply with Quote
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

20 Posts

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

xhostx
Constraint Violating Yak Guru

USA
261 Posts

Posted - 06/29/2012 :  10:04:10  Show Profile  Reply with Quote
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
--------------------------

Edited by - xhostx on 06/29/2012 10:08:02
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000