Is there a way to return a value instead of blank for SQL?

64 Views Asked by At

I am trying to do a HTML table but i am having troubles where it does not return a value. This example shows my result.Sample Image

Is it possible to script that if there isnt any result, it will return 0?

I tried ISNULL method and it does not return me any values.

I am expecting that by using the ISNULL method, it will return a 0 in the result, but it doesn't do so.

SELECT
    ISNULL(body, 0) 
FROM
    [EMAIL_MATRIX] 
WHERE
    body NOT IN (SELECT c.Body
                 FROM [dbo].[EMAIL_DETAILS] a
                 INNER JOIN [Email_matrix] c ON SUBSTRING(a.Subject, 5, LEN(a.Subject) - 4) = c.Subject
                                             AND a.Body LIKE CONCAT('%', c.body, '%') 
                 WHERE CAST(a.Send AS DAte) = '2022-10-04')
2

There are 2 best solutions below

2
milindgv94 On

Try this,

Select REPLACE(ISNULL(body,0),'',0) from [EMAIL_MATRIX] 
where body NOT IN(
SELECT c.Body
FROM [dbo].[EMAIL_DETAILS] a
INNER JOIN [Email_matrix] c 
ON SUBSTRING(a.Subject,5,LEN(a.Subject)-4) = c.Subject
 and a.Body LIKE CONCAT('%', c.body, '%') 
where Cast(a.Send as DAte) = '2022-10-04')
0
gotqn On

Try this:

WITH DataSource AS
(
    Select ISNULL(body,0) AS body
    from [EMAIL_MATRIX] 
    where body NOT IN
    (
        SELECT c.Body
        FROM [dbo].[EMAIL_DETAILS] a
        INNER JOIN [Email_matrix] c 
            ON SUBSTRING(a.Subject,5,LEN(a.Subject)-4) = c.Subject
            and a.Body LIKE CONCAT('%', c.body, '%') 
        where Cast(a.Send as DAte) = '2022-10-04'
    )
)
SELECT body
FROM DataSource
WHERE EXISTS(SELECT 1 FROM DataSource)
UNION ALL
SELECT NULL
WHERE NOT EXISTS(SELECT 1 FROM DataSource)