0

I need to display a sql column Amount in a format of 15 Integers and 3 decimal places (18 length).

  1. Add extra zero's dynamically to complete the length 18
  2. Decimal(.) will also be replaced by a Zero

For Example :

$2,494.60 will be displayed as 000000002494060

Is it possible to achieve it using SQL ?

2 Answers 2

1

You can use STR to fix the leading and trailing decimal places, and REPLACE to change spaces and dots into zeroes.

Query:

SELECT Amount
    , PaddedAmount = REPLACE(REPLACE(STR(Amount, 15, 2), '.', '0'), ' ', '0')
FROM
    (VALUES (12345.678), (1231312), (0.12345), (0), (123456789012.45)) AS V (Amount)

Output:

Amount               PaddedAmount
-------------------  ----------------
12345.67800          000000012345068
1231312.00000        000001231312000
0.12345              000000000000012
0.00000              000000000000000
123456789012.45000   123456789012045
Sign up to request clarification or add additional context in comments.

Comments

0
declare @amount numeric(15,3) = 2949.60
select Right('000000000000000' + replace(cast(@amount as varchar(15)),'.','0'),15)

3 Comments

Can you please recheck end of second line. I think it has some issue
the bracket was facing the other way :)
Thanks alot @Jayvee

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.