Home > Mobile >  How to use concat_ws function from mssql with boolean column?
How to use concat_ws function from mssql with boolean column?

Time:08-13

I using concat_ws to concat multipul colums in MSSQL:

SELECT 
    first_name, 
    last_name, 
    CONCAT_WS(', ', last_name, first_name) tags
FROM 
    customers

I also have is_male column which is a bit (boolean) and I want to add it to concat_ws.

The problem is I got the value 0 or 1 without any indication about "what is this value".

I mean, how to using concat_ws to get the boolean if it exist? something like that:

tags
John Wick male

I was try to do those without success:

select concat_ws(' ', first_name, last_name, if is_male, 'male', '') as tags, *

select concat_ws(' ', first_name, last_name, concat(if is_male, 'male', '')) as tags, *

select concat_ws(' ', first_name, last_name, concat(if is_male = 1, 'male', '')) as tags, *

What else can I do?

CodePudding user response:

You could use IIF

select concat_ws(' ', first_name, last_name, 
                                  IIF(is_male = 1, 'male', 'female')) as tags

or with case:

select concat_ws(' ', first_name, last_name, 
                                  case when is_male = 1 then 'male' else 'female' end) as tags
  • Related