Question : MS SQL 2005 case problem

I have a procedure that has Combine all reason fields into one comma delimited column.The problem is that the I see the results using a lot of commas for example:
,,,,,6,7,8,,
I'm trying to combine 10 different fields into one and I want to display comma if the value is not null.

see the code below

thank you

Here is a example that works:
ISNULL(
      ((isnull(MHDBipolar,''))

       +(case when MHDBipolar is not null then(isnull(+','+MHDDepression,''))else
        (isnull(MHDDepression,''))end)

       + (case when MHDDepression is not null or MHDBipolar is not null then(isnull(+','+MHDSchizophrenia,''))else
        (isnull(MHDSchizophrenia,''))end)

      + (case when MHDDepression is not null or MHDSchizophrenia is not null or MHDBipolar is not null then(isnull(+','+MHDOther,''))else
        (isnull(MHDOther,''))end)
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
/*  Combine all reason fields into one comma delimited column  IT IS showing all the commas*/
  select 
	ISNULL(
	((isnull(r.ReasonsForReferralAbscesses,''))
	
	+(case when r.ReasonsForReferralAbscesses is not null then(isnull(+','+r.ReasonsForReferralCellulitis,''))else
	  	(isnull(r.ReasonsForReferralCellulitis,''))end)

	+ (case when r.ReasonsForReferralAbscesses is not null or r.ReasonsForReferralCellulitis is not null 
		then(isnull(+','+r.ReasonsForReferralDiabetes,''))
		else(isnull(r.ReasonsForReferralDiabetes,''))end)
	+ (case when r.ReasonsForReferralAbscesses is not null 
		or r.ReasonsForReferralCellulitis is not null 
		or r.ReasonsForReferralDiabetes is not null 
		then(isnull(+','+r.ReasonsForReferralFractures,''))
		else(isnull(r.ReasonsForReferralFractures,''))end)
	+ (case when r.ReasonsForReferralAbscesses is not null 
		or r.ReasonsForReferralCellulitis is not null 
		or r.ReasonsForReferralDiabetes is not null
		or r.ReasonsForReferralFractures is not null
		then(isnull(+','+r.ReasonsForReferralMusculoskeletal,''))
		else(isnull(r.ReasonsForReferralMusculoskeletal,''))end)
	+ (case when r.ReasonsForReferralAbscesses is not null 
		or r.ReasonsForReferralCellulitis is not null 
		or r.ReasonsForReferralDiabetes is not null
		or r.ReasonsForReferralFractures is not null
		or r.ReasonsForReferralMusculoskeletal is not null
		then(isnull(+','+r.ReasonsForReferralPneumonia,''))
		else(isnull(r.ReasonsForReferralPneumonia,''))end)
	+ (case when r.ReasonsForReferralAbscesses is not null 
		or r.ReasonsForReferralCellulitis is not null 
		or r.ReasonsForReferralDiabetes is not null
		or r.ReasonsForReferralFractures is not null
		or r.ReasonsForReferralMusculoskeletal is not null
		or r.ReasonsForReferralPneumonia is not null
		then(isnull(+','+r.ReasonsForReferralPostSugical,''))
		else(isnull(r.ReasonsForReferralPostSugical,''))end)
	+ (case when r.ReasonsForReferralAbscesses is not null 
		or r.ReasonsForReferralCellulitis is not null 
		or r.ReasonsForReferralDiabetes is not null
		or r.ReasonsForReferralFractures is not null
		or r.ReasonsForReferralMusculoskeletal is not null
		or r.ReasonsForReferralPneumonia is not null
		or r.ReasonsForReferralPostSugical is not null
		then(isnull(+','+r.ReasonsForReferralSkinUlcer,''))
		else(isnull(r.ReasonsForReferralSkinUlcer,''))end)
	+ (case when r.ReasonsForReferralAbscesses is not null 
		or r.ReasonsForReferralCellulitis is not null 
		or r.ReasonsForReferralDiabetes is not null
		or r.ReasonsForReferralFractures is not null
		or r.ReasonsForReferralMusculoskeletal is not null
		or r.ReasonsForReferralPneumonia is not null
		or r.ReasonsForReferralPostSugical is not null
		or r.ReasonsForReferralSkinUlcer is not null
		then(isnull(+','+r.ReasonsForReferralViralSyndrome,''))
		else(isnull(r.ReasonsForReferralViralSyndrome,''))end)
	+ (case when r.ReasonsForReferralAbscesses is not null 
		or r.ReasonsForReferralCellulitis is not null 
		or r.ReasonsForReferralDiabetes is not null
		or r.ReasonsForReferralFractures is not null
		or r.ReasonsForReferralMusculoskeletal is not null
		or r.ReasonsForReferralPneumonia is not null
		or r.ReasonsForReferralPostSugical is not null
		or r.ReasonsForReferralSkinUlcer is not null
		or r.ReasonsForReferralViralSyndrome is not null
		then(isnull(+','+r.ReasonsForReferralOther,''))
		else(isnull(r.ReasonsForReferralOther,''))end)
	),''
	) as MedicalCondition,

INTO #TempTable

from tbl_Reason r

Answer : MS SQL 2005 case problem


  select
     CASE WHEN LEFT ( isnull(r.ReasonsForReferralAbscesses,'')+ isnull(','+r.ReasonsForReferralCellulitis,'')+isnull(','+r.ReasonsForReferralDiabetes,'')
      +isnull(','+r.ReasonsForReferralFractures,'')+isnull(','+r.ReasonsForReferralMusculoskeletal,'')+isnull(','+r.ReasonsForReferralPneumonia,'')
      +isnull(','+r.ReasonsForReferralPostSugical,'') +isnull(','+r.ReasonsForReferralSkinUlcer,'') +isnull(','+r.ReasonsForReferralViralSyndrome,'')
      +isnull(','+r.ReasonsForReferralOther,''),1) = ','  
      THEN  SUBSTRING( isnull(r.ReasonsForReferralAbscesses,'')+ isnull(','+r.ReasonsForReferralCellulitis,'')+isnull(','+r.ReasonsForReferralDiabetes,'')
      +isnull(','+r.ReasonsForReferralFractures,'')+isnull(','+r.ReasonsForReferralMusculoskeletal,'')+isnull(','+r.ReasonsForReferralPneumonia,'')
      +isnull(','+r.ReasonsForReferralPostSugical,'') +isnull(','+r.ReasonsForReferralSkinUlcer,'') +isnull(','+r.ReasonsForReferralViralSyndrome,'')
      +isnull(','+r.ReasonsForReferralOther,'') , 2, 8000)
      ELSE
      isnull(r.ReasonsForReferralAbscesses,'')+ isnull(','+r.ReasonsForReferralCellulitis,'')+isnull(','+r.ReasonsForReferralDiabetes,'')
      +isnull(','+r.ReasonsForReferralFractures,'')+isnull(','+r.ReasonsForReferralMusculoskeletal,'')+isnull(','+r.ReasonsForReferralPneumonia,'')
      +isnull(','+r.ReasonsForReferralPostSugical,'') +isnull(','+r.ReasonsForReferralSkinUlcer,'') +isnull(','+r.ReasonsForReferralViralSyndrome,'')
      +isnull(','+r.ReasonsForReferralOther,'')
      END
     
INTO #TempTable
from tbl_Reason r

Random Solutions  
 
programming4us programming4us