Question

I have a situation like this

I got a column with 'money' type, 2 decimal . Example data:(65.00)

I need to add 12 zero / 000000000000 to it so that the output would be like this:

(65.00 convert to 6500) + (000000000000) = 000000006500

Output: 000000006500

How can I achieve this?. Thank you for your help and suggestion

Was it helpful?

Solution

You can do this with a couple of casts, multiplying by 100, and using REPLICATE('0') to pad with the requisite number of zeroes). I'm assuming you DO want up to 2 x trailing decimals, but no more.

DECLARE @value MONEY;
SET @value = 65.123;

DECLARE @intValue BIGINT;
SET @intValue = CAST(@value * 100.0 AS BIGINT);
SELECT REPLICATE('0',12-LEN(@intValue)) + CAST(@intValue AS NVARCHAR(20));

Returns 000000006512

If you need to do this on a set, a CTE can be used for the intermediate step, e.g.

WITH cte AS
(
    SELECT CAST(MoneyField * 100.0 AS BIGINT) AS intValue
    FROM SomeTable
)
SELECT
    REPLICATE('0',12-LEN(cte.intValue)) + CAST(cte.intValue AS NVARCHAR(20))
    FROM cte;

Fiddle here

OTHER TIPS

It is Possible .But output Column should be in the type of varchar(15) .If you want to do further operation of your output you have to convert that into int or whatever

SELECT CONCAT(REPEAT('0',12-LENGTH(65.00)),(65.00*100));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top