Question

I am using the following select to query a date from a database table.

The input (ms) for this query results from an xml string and the stored procedure then loops through all the single values in the xml to return a certain number (integer) for each of them. This works fine so far.

Is there a way that I can return a placeholder number (like 99999) if the input (ms) is empty / nothing ? Currently the below returns 0 in such a case which I cannot use to identify this as 0 can also be a valid result in other cases.

My stored procedure so far:

SELECT ms as date,
    type, 
    (
        SELECT COUNT(calendar_dt)
        FROM        Calendar
        WHERE       day_of_week NOT IN (1, 7)
        AND         calendar_dt > GETDATE()
        AND         calendar_dt <= ms
    ) as bDays
FROM @dates
FOR XML PATH('ms'), ELEMENTS, TYPE, ROOT('ranks')

Many thanks in advance for any help with this, Tim.

Was it helpful?

Solution

If the column "ms" is actually NULL or populated, just use ISNULL.

http://technet.microsoft.com/en-us/library/ms184325.aspx

SELECT ISNULL(ms, 99999) AS date

However, if that column can contain an empty string, which is not the same as NULL, then also use NULLIF.

http://technet.microsoft.com/en-us/library/ms177562.aspx

SELECT ISNULL(NULLIF(ms,''), 99999) AS date

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top