Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
heyy..

I have a sql query(a select part) -


SQL
SELECT
      (isnull(stock.StockName,'')+'-'+isnull(manu.ManufacturerName,'')+'-'+isnull(brand.BrandName,'')+'-'+isnull(model.ModelName,'')) as Stockitems 


In this query , I have seperated each part by '-' symbol, If (ManufacturerName) or (BrandName) is null then I get output like "stockname - - ".


I want to remove the '-' sysmbol if (ManufacturerName) or (BrandName) is null..


Any idea..?
Posted

Use CASE WHEN:
SQL
SELECT(
      isnull(stock.StockName,'') + '-' +
      CASE WHEN manu.ManufacturerName IS NOT NULL THEN '-' + manu.ManufacturerName ELSE '' END +
      CASE WHEN brand.BrandName IS NOT NULL THEN '-' + brand.BrandName ELSE '' END +
      CASE WHEN model.ModelName IS NOT NULL THEN '-' + model.ModelName ELSE '' END
      ) AS Stockitems
 
Share this answer
 
Comments
aravindnass 17-Jan-14 6:31am    
thankss.....
OriginalGriff 17-Jan-14 6:40am    
You're welcome!
Try this:

Select ProjectName + Description From
(
select
CASE WHEN
ProjectName IS NOT NULL THEN ProjectName + ' - ' END As ProjectName
,CASE WHEN
Description IS NOT NULL THEN Description + ' - ' END As Description
from project
) temptable

Hope, I will help you.
 
Share this answer
 
Comments
aravindnass 17-Jan-14 6:32am    
thanks
This is not pretty but it should work:
SQL
select  
(
case isnull(stock.StockName,'') 
when '' then '' else stock.StockName + '-' end
+
case isnull(manu.ManufacturerName,'') 
when '' then '' else manu.ManufacturerName + '-' end
+
case isnull(brand.BrandName,'') 
when '' then '' else brand.BrandName + '-' end
+
case isnull(model.ModelName,'') 
when '' then '' else model.ModelName + '-' end
) Stockitems
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900