on 2016 Mar 17 2:45 AM
Here is a simple example:
BEGIN
DECLARE three INTEGER;
SET three = 3;
-- Working
SELECT CAST(32.3369 as DECIMAL(30, 3));
-- Not working
--SELECT CAST(32.3369 as DECIMAL(30, three));
END
Is there a way to cast a decimal to a variable scale?
Business case: Some currency amounts are rounded to 3 decimals in some cultures, others to 2.
Request clarification before answering.
BEGIN
DECLARE three INTEGER;
SET three = 3;
-- Working
SELECT CAST(32.3369 as DECIMAL(30, 3));
-- Working :)
SELECT Round(32.3369, three), Truncnum(32.3369, three) ;
END
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
EXECUTE IMMEDIATE is your friend here if you want to use CAST with a DECIMAL and a variable scale.
BEGIN DECLARE three INTEGER; SET three = 3; -- Working SELECT CAST(32.3369 as DECIMAL(30, 3)); -- Now working EXECUTE IMMEDIATE 'SELECT CAST(32.3369 as DECIMAL(30, ' || three || '))'; END
For ROUND(), Dmitri's approach is strongly recommended...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
52 | |
6 | |
5 | |
5 | |
5 | |
4 | |
3 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.