I have 4 value in a column. For example;
ColumnA
2.651
3.5
4.55
7.9
And I want to sum it all and cast as a decimal;
select CAST(sum(ColumnA) as Decimal(LEN(sum(ColumnA)),2)) FROM tablename;
however I am getting this error:
Msg 102, Level 15, State 1, Server dbrank-tsql, Line 7
Incorrect syntax near '('.
When I just run select len(sum(columnA)) which is 5 and type it to;
select CAST(sum(ColumnA) as Decimal(5,2)) FROM tablename;
it is working. But I cannot always first find the length of this number and then type it to cast.
How can I solve this problem?