Concatenating results from SQL queries and NULL columns
I need to concatenate multiple columns of a table into one value and then show that value in the asp dropdown. The SQL I am issuing looks like this:
SELECT UserID, CustomerNum, UserName + '-' + UserAddress + ',' + UserCity + '' + UserState AS UserInfo FROM users WHERE (CustomerNum = @CustomerNum) ORDER BY UserName
Then I set "UserInfo" as the textbox in the dropdown.
This usually works except when one of the columns in the database is null (like UserState). When this happens, the entire concatenation is null and I get an empty entry in the dropdown.
Is there something in SQLServer that will allow me to ignore these NULL results, or do I need to code something in the DataBind event?
thanks
a source to share
For SQL Server, you have three options:
- IsNull . This is the oldest and most compatible method, although it does not exist in SQL Server Compact Edition (not sure if it is). It takes two arguments and returns the first of the two values, which is not null, or null if both.
- Coalesce . It is newer and preferred for new design. Similar
IsNull
, but can take more than two arguments. LikewiseIsNull
, it will return the first non-empty argument, or null if that's all. - CONCAT_NULL_YIELDS_NULL . This is a database parameter that can be set to
ON
orOFF
. The meaning should be self-explanatory, but here's the MSDN link .
a source to share
Use NULL concatenation to your advantage, it will remove unnecessary separator characters:
SELECT
UserID, CustomerNum
,ISNULL(UserName+' - ','')
+ISNULL(UserAddress+', ','')
+ISNULL(UserCity+' ','')
+ISNULL(UserState,'') AS UserInfo
FROM Users
WHERE CustomerNum = @CustomerNum
ORDER BY UserName
working example:
DECLARE @Users table (userID int, CustomerNum int,UserName varchar(20), UserAddress varchar(20), UserCity varchar(20), UserState varchar(20))
INSERT @Users VALUES (1,111,'Sam','123 First St.', 'city name', 'state name')
INSERT @Users VALUES (2,111,null,'123 First St.', 'city name', 'state name')
INSERT @Users VALUES (3,111,'Sam',null, 'city name', 'state name')
INSERT @Users VALUES (4,111,'Sam','123 First St.', null, 'state name')
INSERT @Users VALUES (5,111,'Sam','123 First St.', 'city name', null)
INSERT @Users VALUES (6,111,null,null, 'city name', 'state name')
SELECT
UserID, CustomerNum
,ISNULL(UserName+' - ','')
+ISNULL(UserAddress+', ','')
+ISNULL(UserCity+' ','')
+ISNULL(UserState,'') AS UserInfo
FROM @Users
--WHERE CustomerNum = @CustomerNum
ORDER BY userID
OUTPUT:
UserID CustomerNum UserInfo
----------- ----------- -------------------------------------------
1 111 Sam - 123 First St., city name state name
2 111 123 First St., city name state name
3 111 Sam - city name state name
4 111 Sam - 123 First St., state name
5 111 Sam - 123 First St., city name
6 111 city name state name
(6 row(s) affected)
a source to share
If you want to ignore null results (exclude them), you can add what I have below to yours WHERE
. ISNULL
or COALESCE
can be used to select blank rows for null columns as described above if that's what you need to do.
...
WHERE UserName is not null
AND UserAddress is not null
AND UserCity is not null
AND UserState is not null
a source to share