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 2005 Forums
 Transact-SQL (2005)
 Shifting Decimal

Author  Topic 

madscientist
Starting Member

30 Posts

Posted - 2008-02-06 : 11:54:03
Hello everyone,

Can anyone please help me with a problem I'm having difficulty figuring out. I have a field named [FINAL_RATE] which is a float with values including 0, 0.34, 0.038, etc.

I would like to move the decimal over to the right in all cases possible whether there is 0, 1, 2, 3, etc. decimal places.

Thank you for the help.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-06 : 12:08:23
try:-
UPDATE Table
SET FINAL_RATE=FINAL_RATE * POWER(10,LEN(LEFT(REVERSE(CAST(FINAL_RATE as varchar(10))),CHARINDEX('.',REVERSE(CAST(FINAL_RATE as varchar(10))))-1)))
Go to Top of Page

madscientist
Starting Member

30 Posts

Posted - 2008-02-06 : 12:46:11
thank you for your response visakh16

but when i execute the query i get an error message:

"Invalid length parameter passed to the SUBSTRING function."
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-06 : 12:56:57
change it like this & try:-
UPDATE Table
SET FINAL_RATE=FINAL_RATE * POWER(10,LEN(LEFT(REVERSE(CAST(FINAL_RATE as varchar(10))),CASE WHEN CHARINDEX('.',REVERSE(CAST(FINAL_RATE as varchar(10))))>0 THEN CHARINDEX('.',REVERSE(CAST(FINAL_RATE as varchar(10)))) ELSE 1 END -1)))
Go to Top of Page

madscientist
Starting Member

30 Posts

Posted - 2008-02-06 : 13:07:56
It worked. Thank you very much visakh16 for all of your help!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-06 : 13:15:42
You are welcome :)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-07 : 07:33:31
I am not sure if this works

Select replace(FINAL_RATE,'.','') as FINAL_RATE from yourtable

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -