Hi
I have below select statement
select salesno,'0' , '0',saleamount from sales
i want 0 to display Blank Space.
Thanks
Hi
I have below select statement
select salesno,'0' , '0',saleamount from sales
i want 0 to display Blank Space.
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Jan 16, 2024, 12:25 AM EST
Your question is not cleared. If you want to show empty/black space instead of '0' then you can use CASE statement like below otherwise improve your question.
SELECT salesno, CASE WHEN '0' = 0 THEN '' ELSE '0' END AS column1, CASE WHEN '0' = 0 THEN '' ELSE '0' END AS column2, saleamount FROM sales;Ramco RamcoRank #462 · 5.4K reputationPosted Jan 16, 2024, 12:56 AM EST
Hi naimish
Below is the hardcoed query . I have used union. When i write '' in place of 0 then it gives error "Error converting datatype varchar to numeric.
Thanks
Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 16, 2024, 2:42 AM EST
To display a blank space instead of the value 0 in a SQL select statement, you can use the
CASEstatement. Here's an example:SELECT salesno, CASE WHEN column1 = 0 THEN '' ELSE column1 END AS column1, CASE WHEN column2 = 0 THEN '' ELSE column2 END AS column2, saleamount FROM sales;Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 16, 2024, 12:22 AM EST
Please use below query.
SELECT salesno, CASE WHEN '0' THEN '' ELSE '0' END, CASE WHEN '0' THEN '' ELSE '0' END, saleamount FROM sales;Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 16, 2024, 1:09 AM EST
The error you’re seeing is because SQL Server is trying to convert an empty string
''to a numeric data type, which is not possible. When you useUNION, SQL Server requires that the data types of the corresponding columns in theSELECTstatements match.If you want to replace
'0'with a blank space in a numeric column when usingUNION, you’ll need to convert all the numeric columns toVARCHARorNVARCHAR. Here’s an example:SELECT CAST(salesno AS NVARCHAR(50)), CASE WHEN column1 = '0' THEN '' ELSE CAST(column1 AS NVARCHAR(50)) END, CASE WHEN column2 = '0' THEN '' ELSE CAST(column2 AS NVARCHAR(50)) END, CAST(saleamount AS NVARCHAR(50)) FROM salesIn this query, replace
column1andcolumn2with the actual column names where you want to replace'0'with a blank space. This query will check each row ofcolumn1andcolumn2, and if the value is'0', it will replace it with an empty string''; otherwise, it will keep the original value.Please note that this will convert your numeric columns to string columns, which might affect any subsequent numeric operations you want to perform on these columns. If you need to perform numeric operations on these columns after the
UNION, you might need to convert them back to a numeric data type.Thanks
Amit MohantyRank #112 · 20K reputationPosted Jan 16, 2024, 1:18 AM EST
Hey Ramco, can you provide your full query along with union?