pink
pink

Reputation: 7

Excel sql db query with case statement

I am new to sql, especially when adding it into a query in Excel. I have three columns in a table (firstname,lastname, field2). Field2 contains yes/no values. I want in excel the query to pull from the table only the firstname and lastname columns, and if that field2 value contains a no, add text to say no after the last name.

select firstname, case field2 when ''No'' then lastnamewanting to add text here to follow last name. ex smith/No when ''Yes'' then lastname end LastName from table1

I do get an error that the syntax cant be graphically displayed due to an error in excel which is to expect since I know my statement isnt correct. I just dont know what would be the correct way to set this up. Maybe its my tics around yes/no? or do I have the case/when words in the wrong places?

Upvotes: 0

Views: 622

Answers (1)

June7
June7

Reputation: 21379

Use IIf(), not CASE. Consider:

If field2 is a Yes/No data type
SELECT firstname, lastname & IIf(field2, "", "/No") AS LN FROM table1;

If field2 is text type
SELECT firstname, lastname & IIf(field2="yes", "", "/No") AS LN FROM table1;

Upvotes: 0

Related Questions