检查字符串值

时间:2011-08-05 13:56:58

标签: sql ms-access

我想在SQL中加入if语句,确定字符串的值是否等于“”(我不是指一个空字符串 - 我的意思是一个两个引号的字符串)。我怎样才能做到这一点。我希望SQL看起来像这样:

Select iif(Answer="","No Response", Answer)
From tblAnswers

10 个答案:

答案 0 :(得分:6)

SELECT CASE
       WHEN Answer = '""' THEN 'No Response'
       ELSE Answer
       END AS Answer
  FROM tblAnswers

答案 1 :(得分:2)

SQL Server:

SELECT ISNULL(NULLIF(Answer, ""), "No Response")

答案 2 :(得分:2)

尝试:

SELECT CASE WHEN Answer = '' THEN 'No Response' ELSE Answer END FROM tblAnswers

答案 3 :(得分:2)

看起来你正在使用Microsoft Access(通过使用iif判断)。要逃避双引号,您应该能够:

Select iif(Answer = """""", "No Response", Answer)
From tblAnswers

答案 4 :(得分:2)

Access'数据库引擎将识别用单引号或双引号括起来的字符串文字。所以虽然这个会起作用......

Select IIf(Answer = """""", "No Response", Answer)
From tblAnswers;
单引号会让它变得更加清晰,IMO,当我的眼睛疲惫时,我不太可能忘记正确数量的双引号。所以我会这样做......

Select IIf(Answer = '""', 'No Response', Answer)
From tblAnswers;

如果您还想在Answer为Null或空字符串时输出 No Response ,请尝试此操作...

Select IIf(Answer = '""' OR Len(Answer & "") = 0, 'No Response', Answer)
From tblAnswers;

答案 5 :(得分:0)

您需要使用转义字符。在SQL中我相信它会是

Answer = "\"\"";

答案 6 :(得分:0)

试试这个:

select if(Answer='""', 'No Response', Answer) as Answer
From tblAnswers

确切的语法会因您的实际数据库而异。如果您的数据库不支持CASE

,则可能需要if

答案 7 :(得分:0)

对于Oracle

select decode(answer,"''''","No response",answer) from tblanswers 

答案 8 :(得分:0)

SELECT ISNULL(NULLIF(Answer,'""'), 'No Response') Answer
FROM tblAnswers

如果列包含两个单引号,请使用:

SELECT ISNULL(NULLIF(Answer, ''''), 'No Response') Answer
FROM tblAnswers

答案 9 :(得分:0)

SELECT IIF(Answer = CHAR(34) & CHAR(34), 'No Response', Answer)
  FROM tblAnswers;