简体   繁体   中英

SQL server, concat nvarchar when some are null

Probably a question of null values...

I would like to do just like in C# :

string s3 = s2 + ' ' + s1;

witch could be read like :

'hello world' = 'hello' + ' ' + 'world

But in SQL, some of my NVARCHAR(1) are null, so it sometimes look like :

null = 'hello + ' ' + null

And in this case i would rather have :

'hello ' = 'hello' + ' ' + null

I don't see, is there a simple way to do that ?

Use COALESCE() or ISNULL() :

SELECT COALESCE(S2, '') + ' ' + COALESCE(S1, '')

Or:

SELECT ISNULL(S2, '') + ' ' + ISNULL(S1, '')

You can also use IIF() function or else CASE Statement

SELECT IIF(s2 IS NULL, '', s2) + ' ' + IIF(s1 IS NULL, '', s1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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