简体   繁体   English

将空白结果设置为等于SQL Server中的特定字符串

[英]Set blank result equal to specific string in SQL Server

I have a query that returns a result set which contains a certain column that needs some tweaking. 我有一个查询,该查询返回的结果集包含需要调整的某些列。 Basically, in the result set, there are certain rows that contain a blank value for the applicable column. 基本上,在结果集中,某些行包含适用列的空白值。 What I need to do is set all instances of that blank value to a specific string. 我需要做的是将该空白值的所有实例设置为特定字符串。 I have tried declaring a variable and setting the variable equal to the column name (using a SELECT statement) and then using an IF statement to set the value to a specific string if it is blank (' '). 我尝试声明一个变量,并将变量设置为等于列名(使用SELECT语句),然后使用IF语句将值设置为特定字符串(如果为空('')。 My code thus far is as follows: 到目前为止,我的代码如下:

declare @sourceNode varchar(30)                  
set @sourceNode = (select sn_name from pt_cust)
if @sourceNode = '' begin
    set @sourceNode = 'None'
end  

This code returns an error stating that the sub-query returns more than 1 value. 此代码返回错误,指出子查询返回的值大于1。 This seems like an easy task but I am stuck at the moment. 这似乎是一项容易的任务,但我目前仍处于困境。 How can this be accomplished? 如何做到这一点?

This is a case (heh) for CASE: 这是CASE的情况(heh):

SELECT CASE WHEN sn_name = '' THEN 'None' ELSE sn_name END
FROM pt_cust

You are getting the error message you mentioned because your SELECT statement can return more than one row, in which case it can not be assigned to a variable. 您收到的错误消息是因为SELECT语句可以返回多个行,在这种情况下,不能将其分配给变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM