SELECT a decimal if NULL show N/A else show USD Amount

683 Views Asked by At

I have a field called Amount type of Decimal(12,2).

Amount, can accept NULL and decimal value example 100.00.

SELECT
NULLIF(Amount, 'N/A') AS Amount
FROM PRODUCTS;

This cause:

Error converting data type varchar to numeric.

I want to SELECT a decimal
if NULL show 'N/A'
else show 'USD Amount'

Hope you can help me. Thanks.

2

There are 2 best solutions below

0
Kavita GK On

Convert Amount to a varchar then apply isnull

SELECT ISNULL(CONVERT(VARCHAR(30),Amount),'N/A') as Amount FROM PRODUCTS;

0
henryloke On

Here the answer:

SELECT IIF(Amount IS NULL, 'N/A', 'USD' + CONVERT(VARCHAR(15),Amount)) as Amount FROM PRODUCTS;

Reference link : SQL Function IIF

Thanks.