I am able to use this:
select COALESCE(my_integer_field, '') from my_table
to display zeros for NULL
values. How can I not to display anything (no zeros) for NULL
or zero values, without resorting to stored procedures?
The only way you can accomplish what you want is to cast/convert your column to a character datatype so you have an empty string.
How to blank out a date field using SQL? "NULL" can be specified as a value in the Date field to get an empty/blank by using INSERT statement.
A NULL value is not same as zero or a blank space. A NULL value is a value which is 'unavailable, unassigned, unknown or not applicable'. Whereas, zero is a number and blank space is a character.
In particular, null values must be distinguished from blank values: A null database field means that there is no value for a given record. It indicates the absence of a value. A blank database field means that there is a value for a given record, and this value is empty (for a string value) or 0 (for a numeric value).
It looks like a job for presentation layer not DB.
Anyway you could use:
SELECT CASE WHEN my_integer_field = 0 OR my_integer_field IS NULL THEN ''
ELSE CAST(my_integer_field AS VARCHAR(10))
END
FROM my_table;
or using IIF
(SQL Server 2012+):
SELECT IIF(my_integer_field = 0 OR my_integer_field IS NULL,
'',
CAST(my_integer_field AS VARCHAR(10)))
FROM my_table;
LiveDemo
Keep in mind that blank
(empty string ''
) will change the datatype of INT
to string.
Or even shorter:
SELECT *,COALESCE(NULLIF(CAST(my_integer_field AS VARCHAR(10)) ,'0'), '') AS result
FROM my_table;
LiveDemo2
Output:
╔══════════════════╦════════╗
║ my_integer_field ║ result ║
╠══════════════════╬════════╣
║ NULL ║ ║
║ 0 ║ ║
║ 1 ║ 1 ║
║ 2 ║ 2 ║
╚══════════════════╩════════╝
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With