Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can we use ISNULL to all Column Names in SQL Server 2008?

I have a question

I tried to google it but looks like they don't like *

I'm using SQL Server 2008.

I have the following database table:

 P_Id   ProductName UnitPrice   UnitsInStock    UnitsOnOrder
------------------------------------------------------------------------
   1    Jarlsberg   10.45                 16    15
   2    Mascarpone  Null                  23    NULL 
   3    Gorgonzola  15.67                  9    20

If I need to replace the null with a string I know I do :

 SELECT ISNULL(UnitsOnOrder,'No Data') FROM tbl

Questions

  • How can I use ISNULL() with multi column names ?
  • is it possible to use it with *

Like

SELECT ISNULL(* , 'NO data') FROM tbl 

I think this will be tricky because of the datatype, you can't pass string to INT datatype so how can I fix this too

Update

Okay if i use ISNULL() with a datatype of int it will return 0 which will be a value to me , how can i pass empty string instead ?

like image 892
Mina Gabriel Avatar asked Nov 05 '12 18:11

Mina Gabriel


People also ask

What is the use of ISNULL in SQL?

SQL Server ISNULL () function overview The SQL Server ISNULL () function replaces NULL with a specified value. The following shows the syntax of the ISNULL () function:

How to replace null with a specified value in SQL Server?

The SQL Server ISNULL () function replaces NULL with a specified value. The following shows the syntax of the ISNULL () function:

What is a NULL column value in SQL Server?

It is important to understand that a NULL column value is different than having a blank (empty string) or 0 value in a column. eg. ' ' or (empty string) <> NULL, 0 <> NULL. Let’s take a look at a few examples to illustrate these points.

What is the difference between coalesce () and ISNULL () in SQL?

Therefore, The ISNULL () SQL function looks at the first value and the second parameter value is automatically limited to that length furthermore, but the COALESCE () SQL function does not have this restriction. In the following given above image output, the test variable has length 2.


2 Answers

You can use ISNULL multiple times in the same SQL statement for different columns, but you must write it separately for each column:

SELECT
    ISNULL(ProductName, 'No Data') AS ProductName,
    ISNULL(CAST(UnitPrice AS NVARCHAR), 'No Data') AS UnitPrice, 
    ISNULL(CAST(UnitsInStock AS NVARCHAR), 'No Data') AS UnitsInStock,
    ISNULL(CAST(UnitsOnOrder AS NVARCHAR), 'No Data') AS UnitsOnOrder
FROM tbl

If you are building a dynamic SQL query, you could theoretically gather a list of columns in the table and generate a query with ISNULL on each one. For example:

DECLARE @SQL nvarchar(max)

SET @SQL = 'SELECT '

SELECT @SQL = @SQL + 'ISNULL(CAST([' + sc.name + '] AS NVARCHAR), ''No Data'') AS [' + sc.name + '],'
FROM sys.objects so
INNER JOIN sys.columns sc ON sc.object_id = so.object_id
WHERE so.name = 'tbl'

-- Remove the trailing comma
SELECT @SQL = LEFT(@SQL, LEN(@SQL) - 1) + ' FROM tbl'

EXEC sp_sqlexec @SQL

This code has problems when converting some column types like timestamps to an nvarchar, but it illustrates the technique.

Note that if you had another column that should be returned if a value is null, you could use the COALESCE expression like this:

SELECT COALESCE(ProductName, P_Id) AS Product...
like image 196
Paul Williams Avatar answered Oct 19 '22 02:10

Paul Williams


Try this...

ISNULL (COALESCE (column1, column2), 'No Data')

You would need to include all column names though, you can't use *

COALESCE returns the first non-null value in its argument list so if they are all null it will return null

like image 2
Rich Andrews Avatar answered Oct 19 '22 03:10

Rich Andrews