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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 If empty select different field

Author  Topic 

scripter
Starting Member

49 Posts

Posted - 2009-04-11 : 11:56:20
ok in the below query if I want the following lines changed
,p.[Alias]
,p.[FirstName]
,p.[LastName]

Instead I want to combine those fields into 1 field and if the p.[Alias] is empty then combin p.FirstName and p.LastName with a space between them

give this new field an alias of Names


so it will have the Alias if there is one but if not it will have first and last name instead.



SELECT f.[ProducerID]
,p.[Alias]
,p.[FirstName]
,p.[LastName]
,[Match_Score]
,[Match_PreferredFirmState]
,[Match_LicensedAllStates]
,[Match_PreferredFirmCity]
,[Match_PayoutID]
,[Match_U4CRD]
,[Match_RIAADV]
,[Match_BrandOffice]
,[Match_OfficeSpace]
,[Match_GroupHealth]
,[Match_RetirementPlan]
,[Match_PracticeID]
,[Match_ClassificationID]
,[Match_ProductionAmount]
,[Match_ExperienceID]
,[Match_FirmClearing]
FROM [ASPNETDB].[dbo].[fn_Find_Producer_Match_Score](100022) f
JOIN [ASPNETDB].[dbo].[Producer] p
ON f.ProducerID = p.ProducerID
ORDER BY [Match_Score] DESC

scripter
Starting Member

49 Posts

Posted - 2009-04-11 : 12:25:59
Nevermind I figured it out


SELECT f.[ProducerID]
,(CASE WHEN p.[Alias] ='' then p.[FirstName] + ' ' + p.[LastName] ELSE p.[Alias] END) as Names
,[Match_Score]
,[Match_PreferredFirmState]
,[Match_LicensedAllStates]
,[Match_PreferredFirmCity]
,[Match_PayoutID]
,[Match_U4CRD]
,[Match_RIAADV]
,[Match_BrandOffice]
,[Match_OfficeSpace]
,[Match_GroupHealth]
,[Match_RetirementPlan]
,[Match_PracticeID]
,[Match_ClassificationID]
,[Match_ProductionAmount]
,[Match_ExperienceID]
,[Match_FirmClearing]
FROM [ASPNETDB].[dbo].[fn_Find_Producer_Match_Score](100022) f
JOIN [ASPNETDB].[dbo].[Producer] p
ON f.ProducerID = p.ProducerID
ORDER BY [Match_Score] DESC
Go to Top of Page

Rajesh Jonnalagadda
Starting Member

45 Posts

Posted - 2009-04-13 : 06:00:05
Hi,
It is better to use like this,

,(CASE WHEN ISNULL(p.[Alias],'') ='' OR p.[Alias] ='' then p.[FirstName] + ' ' + p.[LastName] ELSE p.[Alias] END) as Names



Rajesh Jonnalagadda
[url="http://www.ggktech.com
"]GGK TECH[/url]
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-04-13 : 08:54:01
hi use ISNULL OR coalesce function than case ........
Go to Top of Page
   

- Advertisement -