@Janice Chi If you are looking for guidance on formatting decimals in Azure SQL to preserve leading zeros. You can use the FORMAT syntax. Check the examples below and let us know if that help in anyways.DB2
Use VARCHAR_FORMAT()
with a numeric format string:
sql
-- Preserves leading zero
SELECT VARCHAR_FORMAT(DECIMAL(0.0123, 10, 4), '0.############################') AS formatted
-- Output: '0.0123'
SELECT VARCHAR_FORMAT(DECIMAL(-0.045, 10, 3), '0.############################') AS formatted
-- Output: '-0.045'
This ensures:
Leading zero before decimal
No trailing zeros
Works for both positive and negative values
Azure SQL
Use FORMAT()
with a matching format string:
sql
-- Preserves leading zero
SELECT FORMAT(CAST(0.0123 AS DECIMAL(10,4)), '0.############################') AS formatted
-- Output: '0.0123'
SELECT FORMAT(CAST(-0.045 AS DECIMAL(10,3)), '0.############################') AS formatted
-- Output: '-0.045'
Alternatively, for fixed precision:
sql
SELECT FORMAT(CAST(0.0123 AS DECIMAL(10,4)), '0.0000') AS formatted
-- Output: '0.0123'