0

i have problem.., i want to display the null value as 'X' i has been using the case statement but still failed, i already using Isnull and failed too.

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
DECLARE @X AS NVARCHAR(MAX)



SELECT @ColumnName= ISNULL(@ColumnName + ',','') + QUOTENAME(code)
FROM (SELECT DISTINCT code FROM edrsDB..tbl_users where area='west' ) AS status

SET @DynamicPivotQuery = 
N'SELECT date, '+ @ColumnName +' 
FROM (
SELECT DISTINCT userid, status , DATEPART(dd, [date]) as Date
FROM edrsDB..tbl_status
WHERE DatePart(MM, [Date]) = 3
 And DATEPART(YYYY, [Date]) = 2017
Union
SELECT DISTINCT userid, status , DATEPART(DD,[date]) as Date
FROM edrsDB..tbl_public_holiday
WHERE DatePart(MM, [Date]) = 3
And DATEPART(YYYY, [Date]) = 2017
Union
SELECT DISTINCT userid, status , DATEPART(DD,[Date]) as Date 
FROM edrsDB..tbl_station_weekend
WHERE DatePart(MM, [Date]) = 3
And DATEPART(YYYY, [Date]) = 2017
And date IS NOT NULL
 ) AS x 
 PIVOT
 (
max(status)
FOR userid IN (' + @ColumnName + ')
) AS PVTTable'
 EXEC sp_executesql @DynamicPivotQuery

this is ouput.. , i want null to be display as 'X'

output

Tahi
  • 85
  • 1
  • 10

3 Answers3

1

You can use ISNULL

ISNULL(<some_column/some_stuff>, 'X')

'X' would be the value displayed instead of the first parameter if first parameter result is null

beejm
  • 2,381
  • 1
  • 10
  • 19
0

Use ISNULL function in SQL Server :

SELECT ISNULL(Your_column,'X')
Mansoor
  • 4,061
  • 1
  • 17
  • 27
0

You can use isnull() or coalesce() in sql server.

select isnull(col,'X')

or

select coalesce(col,'X')

The main difference between the two is that coalesce() can support more than 2 parameters, and it selects the first one that is not null. More differences between the two are answered here.

select coalesce(col,col2,'X')

coalesce() is also standard ANSI sql, so you will find it in most RDBMS. isnull() is specific to sql server.

Reference:

Community
  • 1
  • 1
SqlZim
  • 37,248
  • 6
  • 41
  • 59