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.
Author |
Topic |
ricky_1605
Starting Member
30 Posts |
Posted - 2010-10-28 : 02:45:09
|
Hi guysThe stored procedure is as follows:DECLARE @auxReportTable1 TABLE(value1 varchar(500), value2 varchar(500), value3 varchar(500))selectvalue1 = max(CASE WHEN location = 'Vadodara' THEN (AllLocations.Domestic) ELSE 'NA' END),value2 = max(CASE WHEN location = 'Vadodara' THEN (AllLocations.Owners) ELSE 'NA' END), value3 = max(CASE WHEN location = 'Vadodara' THEN (AllLocations.Contractors) ELSE 'NA' END)FROM AllLocations WITH (nolock)WHERE AllLocations.DateId = @IdGROUP BY AllLocations.DateidThe problem is: The data in these columns is fetched properly if the data starts with alphabets n,o,p,q,r,s,t,u,v,w,x,y,z but if the data starts with any other alphabet before n i.e. a-m then it show NA.Please helpThanksNipun Chawla |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-10-28 : 03:01:59
|
Try this logicvalue1 = stuff(max(CASE WHEN location = 'Vadodara' THEN 'Z'+(AllLocations.Domestic) ELSE 'NA' END),1,1,'')MadhivananFailing to plan is Planning to fail |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-10-28 : 03:34:33
|
value1 = COALESCE(max(CASE WHEN location = 'Vadodara' THEN (AllLocations.Domestic) ELSE NULL END), 'NA'),Might give you ANSI Warnings if the expression is NULL though.So long as there is no "valid" empty string in your data you could use:value1 = COALESCE(NullIf(max(CASE WHEN location = 'Vadodara' THEN (AllLocations.Domestic) ELSE '' END), ''), 'NA'),at which point Madhi's suggestion starts to look less Geeky than I first thought!"WITH (nolock)"Shudder! Don't use that. Ever. If you know what I am talking about then fine, if you don't then stop using it, implement READ_COMITTED_SNAPSHOT instead and remove NOLOCK from all your code. |
 |
|
ricky_1605
Starting Member
30 Posts |
Posted - 2010-10-28 : 04:42:54
|
@madhivanan & kristenThanks a lot both the solutions worked.but i still could not understand the function of COALESCE.Nipun Chawla |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-10-28 : 04:45:13
|
COALESCE(Param1, Param2, Param3, ...)returns the first parameter that is NOT NULL |
 |
|
|
|
|
|
|