开发者

Change Select statement in query

开发者 https://www.devze.com 2023-03-25 01:03 出处:网络
I have a table (result of a stored procedure from someone else), and开发者_JS百科 it retuns a table ##tableT in the form

I have a table (result of a stored procedure from someone else), and开发者_JS百科 it retuns a table ##tableT in the form

1      2    3    4    5     6
----------------------------------------------
467    116  480  477  491   697
NULL   19   481  113  488   466
123    4354 477  466  55    480
NULL   NULL NULL 527  83    629
45     34   NULL  5   483   483

As you can see it has NULLS, and most important the name of the columns are numbers, so when I do

SELECT coalesce(1, -1) as att1, coalesce(2, -1) as att2,....,coalesce(6, -1) as att6 FROM [dbm].[dbo].[##tableT];

I get:

1   2   3   4   5   6
1   2   3   4   5   6
1   2   3   4   5   6
1   2   3   4   5   6
1   2   3   4   5   6

Instead of:

1      2    3    4    5     6
----------------------------------------------
467    116  480  477  491   697
-1     19   481  113  488   466
123    4354 477  466  55    480
-1     -1   -1   527  83    629
45     34   -1   5   483    483


try

SELECT ISNULL([1], -1) as att1, ISNULL([2], -1) as att2,....,ISNULL([6], -1) as att6 FROM [dbm].[dbo].[##tableT];


and as a variant

SELECT 
CASE WHEN [1] IS NULL THEN -1 ELSE [1] END,
CASE WHEN [2] IS NULL THEN -1 ELSE [2] END,
...
FROM [dbm].[dbo].[##tableT];
0

精彩评论

暂无评论...
验证码 换一张
取 消