Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Add decimals from other column to the existing

Author  Topic 

Prabhu_K
Starting Member

2 Posts

Posted - 2012-08-05 : 09:00:46
Hi,
I have a requirement, where I need to add the decimals to a column based on the precision/values stored in another column.

Ex:
COL1 COL2 COL3
135 141 1
NULL 0.1 3
NULL 4 2

I need to use COL3 decimal precision and convert the columns 1 and 2 with the right value.

In above case, The result should look like below.
COL1 COL2 COL3
135.0 141.0 1
NULL 0.100 3
NULL 4.00 2

Please let me know any function which can do this or any other option.

Please reply ASAP.


With best Regards
Prabhu

Prabhu_K
Starting Member

2 Posts

Posted - 2012-08-05 : 12:35:13
Any updates please!!

With best Regards
Prabhu
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-08-05 : 13:54:31
As precision is part of data type, when defined, so possibly it would be hard to do that as a batch. But still you can do it through cursor and dynamic query.

DECLARE @Str VARCHAR(500)

SELECT @Str = 'SELECT CAST(COL1 AS DECIMAL (18,'+CAST (@COL3 AS VARCHAR(10))+')) AS COL1,
CAST(COL2 AS DECIMAL (18,'+CAST(@COL3 AS VARCHAR(10))+')) AS COL2
FROM #Test'
EXEC (@Str)

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page
   

- Advertisement -