| Author |
Topic |
|
stumbling
Posting Yak Master
104 Posts |
Posted - 2007-08-10 : 10:27:46
|
| Hi All2 post in a row first time in ages so i must be getting better but i get stuck on crappy little things like this hopefullyI have data in the following format 4.13999999999999974.24000000000000025.48000000000000045.17999999999999976.7699999999999996i want to select the data as such4.134.245.485.176.76I keep chasing my tale on this one around and around any one got a simple idea i am missing, you would not believe how much time i have spent on this.CheersPhil |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2007-08-10 : 10:34:32
|
| Try thisselect substring(convert(varchar(20),4.1399999999999997),1,4)select substring(convert(varchar(20),COLUMN_NAME),1,4)Ashley Rhodes |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-08-10 : 10:42:59
|
| Or thisselect FLOOR(6.7699999999999996 *100)/100 Jim |
 |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2007-08-10 : 11:05:28
|
| my previous solution does not work when selecting from a table and neither does floorCREATE TABLE #Table1 (C1 float) INSERT INTO #Table1 VALUES(4.1399999999999997)INSERT INTO #Table1 VALUES(4.2400000000000002)INSERT INTO #Table1 VALUES(6.7699999999999996)INSERT INTO #Table1 VALUES(5.1799999999999997) INSERT INTO #Table1 VALUES(5.2999999999999997) select cast(cast(C1 as int) as varchar(10)) + cast(substring(cast(C1-cast(C1 as int) as varchar(10)),2,2) as varchar(4))FROM #Table1select floor(C1*100)/100 from #table1This is interesting. Never paid attention to it before.Ashley Rhodes |
 |
|
|
stumbling
Posting Yak Master
104 Posts |
Posted - 2007-08-10 : 11:16:13
|
| This is what im finding is there a way around this? |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-08-10 : 11:45:56
|
If you are getting a rounding "error" then can we assume that you are using a FLOAT data type?FLOAT and REAL are approximations so, I'm not sure there is anyway to get what you want. You could try casting to a NUMERIC..?SELECT CAST(C1 AS NUMERIC(3,2))FROM #Table1 or possibly store the data as a different datat type, like NUMERIC. |
 |
|
|
stumbling
Posting Yak Master
104 Posts |
Posted - 2007-08-10 : 11:53:27
|
quote: Originally posted by Lamprey If you are getting a rounding "error" then can we assume that you are using a FLOAT data type?FLOAT and REAL are approximations so, I'm not sure there is anyway to get what you want. You could try casting to a NUMERIC..?SELECT CAST(C1 AS NUMERIC(3,2))FROM #Table1 or possibly store the data as a different datat type, like NUMERIC.
Yep the retail package stores it as float i can not change this as it is out of my control. Is there a way to use substring and charindex to grab the decimal and then take everything from the start up to 2 characters pas the decimal??? |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2007-08-10 : 12:02:50
|
quote: Originally posted by stumbling This is what im finding is there a way around this?
I doubt it. Floats are imprecise numbers. If you want to do things precisely you will need to use decimal.DECLARE @c1 float ,@c2 floatSELECT @c1 = 5.2999999999999997 ,@c2 = 5.3SELECT @c1, @c2 |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-08-10 : 12:13:42
|
| I used a numeric (25,20) and thisselect floor(C1*100.0)/100 produced this4.1300004.2400006.7600005.1700005.290000Jim |
 |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2007-08-10 : 14:45:08
|
| jimf can u write the query how u used numeric(25,20)Ashley Rhodes |
 |
|
|
stumbling
Posting Yak Master
104 Posts |
Posted - 2007-08-10 : 19:24:52
|
quote: Originally posted by jimf I used a numeric (25,20) and thisselect floor(C1*100.0)/100 produced this4.1300004.2400006.7600005.1700005.290000Jim
Hi Jim can you tell me how you got this :-)Phil |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-08-13 : 06:46:35
|
| I just created the table NUMERIC(25,20), but this works with floatDECLARE @Table1 TABLE (C1 float) INSERT INTO @Table1 VALUES(4.1399999999999997)INSERT INTO @Table1 VALUES(4.2400000000000002)INSERT INTO @Table1 VALUES(6.7699999999999996)INSERT INTO @Table1 VALUES(5.1799999999999997) INSERT INTO @Table1 VALUES(5.2999999999999997) SELECT FLOOR(CONVERT(NUMERIC(25,20),C1)*100)/100 FROM @table1 |
 |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2007-08-13 : 09:35:08
|
| you nailed it finallybut can you explain how numeric(25,20) works and why did you select thiswhen SELECT FLOOR(CONVERT(NUMERIC(38,30),C1)*100)/100 FROM @table1SELECT FLOOR(CONVERT(NUMERIC(38,25),C1)*100)/100 FROM @table1SELECT FLOOR(CONVERT(NUMERIC(28,25),C1)*100)/100 FROM @table1SELECT FLOOR(CONVERT(NUMERIC(32,19),C1)*100)/100 FROM @table1They all and many other combinations give the same result. What if I want to round to three decimal places.Ashley Rhodes |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-08-13 : 09:58:44
|
| I used Numeric (25,20) somewhat arbitrarily. There are 16 places to the right of the decimal, so I knew I needed at least that much. I'd like the SQL gods to opine on what's going on with numeric and rounding. I read a good article about rounding and data types in SQL magazine, but you need a subscription to read it on-line. I haven't found a similar article yet.Jim |
 |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2007-08-13 : 10:40:07
|
| you can paste some parts of that article here if its not copyright restricted. do u need to pay for that subscription.what is that site.Ashley Rhodes |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
|
|
|