Checking a String value
I would like to incorporate into SQL an if statement determining if the value of a string is equal to "" (and I don't mean an empty string - I mean a string of two quotation marks). How can I do this. I would like the SQL to look something like this开发者_开发百科:
Select iif(Answer="","No Response", Answer)
From tblAnswers
SELECT CASE
WHEN Answer = '""' THEN 'No Response'
ELSE Answer
END AS Answer
FROM tblAnswers
It looks like you're using Microsoft Access (judging by the use of iif). To escape double quotes, you should be able to do:
Select iif(Answer = """""", "No Response", Answer)
From tblAnswers
SQL Server:
SELECT ISNULL(NULLIF(Answer, ""), "No Response")
Try:
SELECT CASE WHEN Answer = '' THEN 'No Response' ELSE Answer END FROM tblAnswers
Access' database engine will recognize string literals enclosed in either single or double quotes. So although this one will work ...
Select IIf(Answer = """""", "No Response", Answer)
From tblAnswers;
... single quotes would make it clearer, IMO, and I'd be less likely to lose track of the proper number of double quotes when my eyes are tired. So I'd do this instead ...
Select IIf(Answer = '""', 'No Response', Answer)
From tblAnswers;
If you also want to output No Response when Answer is Null or an empty string, try this ...
Select IIf(Answer = '""' OR Len(Answer & "") = 0, 'No Response', Answer)
From tblAnswers;
You need to use an escape character. In SQL I believe it would be
Answer = "\"\"";
Try this:
select if(Answer='""', 'No Response', Answer) as Answer
From tblAnswers
The exact syntax will vary with your actual database. You may need a CASE
if your DB doesn't support if
For Oracle
select decode(answer,"''''","No response",answer) from tblanswers
SELECT ISNULL(NULLIF(Answer,'""'), 'No Response') Answer
FROM tblAnswers
If the column contains two single quotes, use this:
SELECT ISNULL(NULLIF(Answer, ''''), 'No Response') Answer
FROM tblAnswers
SELECT IIF(Answer = CHAR(34) & CHAR(34), 'No Response', Answer)
FROM tblAnswers;
精彩评论