SQL query to replace a substring
I wish to write a query which will replace the value0.00 with "-" in a column. The value should only be replaced if the value is 0.00 and not i开发者_运维知识库f 230.00
I did this query replace(SUBSTRING(cast(columname AS varchar(7)),0,7),'0.00','-')
but it replaces 230.00 as 23-
Please help
Assuming a table format like so:
`id` | `cost`
1 | 230.00
2 | 543.65
3 | 0.00
Then a query of
SELECT CASE WHEN cost=0 THEN '-' ELSE cost END FROM table;
will return
230.00
543.65
-
use:
if (SUBSTRING(cast(columname AS varchar(7)),0,7) = '0.00', '-', SUBSTRING(cast(columname AS varchar(7)),0,7))
CASE WHEN columname = 0 THEN '0.00' ELSE replace(SUBSTRING(cast(columname AS varchar(7)),0,7),'0.00','-') END
精彩评论