SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Add decimals from other column to the existing
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Prabhu_K
Starting Member

India
2 Posts

Posted - 08/05/2012 :  09:00:46  Show Profile  Reply with Quote
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

India
2 Posts

Posted - 08/05/2012 :  12:35:13  Show Profile  Reply with Quote
Any updates please!!

With best Regards
Prabhu
Go to Top of Page

lionofdezert
Aged Yak Warrior

Pakistan
864 Posts

Posted - 08/05/2012 :  13:54:31  Show Profile  Visit lionofdezert's Homepage  Send lionofdezert a Yahoo! Message  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000