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
 If row = null do something else

Author  Topic 

Blackstar_solar
Starting Member

1 Post

Posted - 2006-12-01 : 06:42:52
Hi,

I currently have this sql statement

SELECT Code + '-' + Name + '-' + LegacyCode AS PrimaryCode, PrimaryCodeId FROM dbo.PrimaryCode ORDER BY Code + '-' + Name + '-' + LegacyCode
[code]
The problem is that if the LegacyCode is null then that row isn't displayed, I just get null back. What I want to do is something like
[code]
if LegacyCode = null [for a particular row] then
SELECT Code + '-' + Name AS ...ORDER BY...
else
SELECT Code + '-' + Name + '-' + LegacyCode AS ...ORDER BY...
End

Problem is I have no clue how to do that

If anyone could shed some light on this I'd really appreciate it.
Thanks

Blackstar_solar

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2006-12-01 : 06:44:57
SELECT Code + '-' + Name + '-' + COALESCE(LegacyCode, '')


Duane.
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2006-12-01 : 07:01:54
Or ISNULL(LegacyCode,'')

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-12-01 : 07:29:10
SELECT Code + '-' + Name + COALESCE('-' + LegacyCode, '')



- Jeff
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-12-04 : 10:00:56
Note that code should be converted to varchar if it is not. Also as it seems concatenation, if you use front end application, you can do there also

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -