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 2000 Forums
 SQL Server Development (2000)
 old school Fractions

Author  Topic 

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2004-02-11 : 12:10:40
I have some Fools here that refuse to learn how to use decimals, They wish to enter and have displayed numbers in combinations with fractions. Oh and the best part is that they are not just storage fields yes they are part of very complex computations.

Has anyone found good ways to deal with Fractions.

Jim
Users <> Logic

X002548
Not Just a Number

15586 Posts

Posted - 2004-02-11 : 12:46:40
You're kidding, right?

You'll need to store 3 values I would guess....

Won't the GUI be more painful than it's worth...

Brett

8-)
Go to Top of Page

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2004-02-11 : 12:49:41
I was thinking the best solution would be a ball bat with large nails in it.

Oh I forgot the best part They want one Field not 3.



Jim
Users <> Logic
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-02-11 : 12:55:53
can you present them with 1 field, and allow them to enter in the GUI as 1 field (i.e, X/Y), but still STORE it internally as numerator/denominator ?

you can always present it anyway they like in views and stored procs.

are you going to reduce it if they enter 2/4 to 1/2 ??

(Brett -- why store 3 values ? what is the 3rd?)

- Jeff
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-02-11 : 13:03:30
quote:
Originally posted by JimL

I forgot the best part They want one Field not 3.



but of course ...so they want to type


"2 1/4"?

So you have to defines it as varchar and the use charindex to find the space and the divinding symbol

And I'm Sure you're going to have to display the results in fractions....that's harder..


DECLARE @x varchar(15), @y int, @z int

SELECT @x = '2 1/4'

SELECT LEFT (@x,CHARINDEX(' ',@x)) AS WholeNumber
, SUBSTRING(@x,CHARINDEX(' ',@x)+1,CHARINDEX('/',@x)-CHARINDEX(' ',@x)-1) AS Numerator
, SUBSTRING(@x,CHARINDEX('/',@x)+1,LEN(@x)-CHARINDEX('/',@x)) As Denominator


You still got your Ranger stuff?



Brett

8-)
Go to Top of Page

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2004-02-11 : 13:05:07
I think your getting at pulling it a part with a substing?

Here is some examples of data.

25 13/16
25 13 / 16
25 13/ 16
25 13 /16
25-13/16
25, 13/16
25 and 13/16
25 & 13/16
25 13/16
25_13/16
25 + 13/16
25.8125

And no I am not Kidding unfortunatly.

unless I can present enough evidence to convince them.

Jim
Users <> Logic
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-02-11 : 13:06:54
quote:
Originally posted by JimL

I think your getting at pulling it a part with a substing?

Here is some examples of data.

25 13/16
25 13 / 16
25 13/ 16
25 13 /16
25-13/16
25, 13/16
25 and 13/16
25 & 13/16
25 13/16
25_13/16
25 + 13/16
25.8125

And no I am not Kidding unfortunatly.

unless I can present enough evidence to convince them.

Jim
Users <> Logic



What the hell is this?

You mean the data already exists?

a scrubbing we will go, a scrubbing we will go, hi ho...you get the picture..



Brett

8-)
Go to Top of Page

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2004-02-11 : 13:08:28
quote:
You still got your Ranger stuff?



Brett

8-)



What do you think?
You can take the Ranger out of the Army but never the Ranger out of the man.

Jim
Users <> Logic
Go to Top of Page

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2004-02-11 : 13:11:26
Oh Yes some Pre existing data.

Can you say fools and excell spread sheets.

Jim
Users <> Logic
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2004-02-11 : 13:20:25
Jim, I'd give some serious consideration to storing the data as a normal numeric and write a couple of functions to convert decimal to fraction for display and fraction to decimal for input. That way all code behind the scenes can stay normal for doing math, etc.

By the way, is the denominator always 16? (Sounds like a bunch of mechanics with SAE socket wrenches... Or somebody with a good old ruler...) If so, the display conversion formula shouldn't be too touch, and even reducing the fraction should work okay. But if it varies like they get into thirds (1/3s) which does not convert nicely into 1/16ths then I really feel sorry for you.

Also, is 25 - 13/16 the same as 25 + 13/16? Or is that a math formula (25 minus 13/16)?

--------------------------------------------------------------
Find more words of wisdom at [url]http://weblogs.sqlteam.com/markc[/url]
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-02-11 : 13:30:00
quote:
Originally posted by AjarnMark

Jim, I'd give some serious consideration to storing the data as a normal numeric and write a couple of functions to convert decimal to fraction for display and fraction to decimal for input. That way all code behind the scenes can stay normal for doing math, etc.

--------------------------------------------------------------
Find more words of wisdom at [url]http://weblogs.sqlteam.com/markc[/url]



Yeah well that where I was kind of going (even if it wasn't obvious)

No other way he'd be able to do the math...

And what about displaying the data? What Denominator yuo going to pick?

And the data is barely in usable format...how much data we talking about?

Better yet, who's going to clean it up?




Brett

8-)
Go to Top of Page

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2004-02-11 : 13:34:01
Not that much data now.

But remember I will have a Varchar field god knows what I will get unless I build some very good masks.

This whole thing is a waste of my time.

No not just 16th any combination of any number and fraction or decimal.


Where did I put that Bat.


Jim
Users <> Logic
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-02-11 : 14:00:02
Well...Sorry dude...

Here's some data scubbing help...HTH


USE Northwind
GO

CREATE TABLE myTable99(x varchar(15))
GO

INSERT INTO myTable99(x)
SELECT '25 13/16' UNION ALL
SELECT '25 13 / 16' UNION ALL
SELECT '25 13/ 16' UNION ALL
SELECT '25 13 /16' UNION ALL
SELECT '25-13/16' UNION ALL
SELECT '25, 13/16' UNION ALL
SELECT '25 and 13/16' UNION ALL
SELECT '25 & 13/16' UNION ALL
SELECT '25 13/16' UNION ALL
SELECT '25_13/16' UNION ALL
SELECT '25 + 13/16' UNION ALL
SELECT '25.8125'
GO

UPDATE myTable99 Set x = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
x,'-',' '),'and',' '),'_',' '),'+',' '),'&',' '),',',' ')
GO

SELECT SUBSTRING(x,1,CHARINDEX(' ',x)-1)
, REPLACE(SUBSTRING(x,CHARINDEX(' ',x)+1,LEN(x)-CHARINDEX(' ',x)+1),' ','')
FROM myTable99
WHERE CHARINDEX(' ',x) > 1
GO

ALTER TABLE myTable99 ADD y float
GO

UPDATE t SET y =
CONVERT(integer,SUBSTRING(x,1,CHARINDEX(' ',x)-1))+
(CONVERT(float,SUBSTRING(x,CHARINDEX(' ',x)+1,CHARINDEX('/',x)-CHARINDEX(' ',x)-1))/
CONVERT(float,SUBSTRING(x,CHARINDEX('/',x)+1,LEN(x)-CHARINDEX('/',x))))
FROM myTable99 t
WHERE CHARINDEX(' ',x) > 1
GO

SELECT * FROM myTable99
GO

DROP TABLE myTable99
GO




Brett

8-)
Go to Top of Page

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2004-02-11 : 14:55:33
Thanks For the help.

And for Listening to me rant and rave.



Jim
Users <> Logic
Go to Top of Page

MikeB
Constraint Violating Yak Guru

387 Posts

Posted - 2004-02-11 : 17:17:10
quote:
Originally posted by JimL

Thanks For the help.

And for Listening to me rant and rave.



Jim
Users <> Logic



I am relatively new here and not a computer guru by any means, but I am not sure I understand the problem. Why would you not just store the decimal equivalent to the fraction and format input / output in the front end ?

If there is existing data, create a new column, convert and insert values into new column.

Please don't take this post the wrong way, I was just looking to see what your concerns are?

Mike B
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-02-11 : 19:26:18
again, why not just store 2 columns? numerator and denominator. you calculate and display it anyway you want , and you don't have to worry about rounding and handling 1/3 as .3333... and all that.

- Jeff
Go to Top of Page

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2004-02-12 : 08:00:54
Bret actually has the right idea. (As usual) (don’t get a big head LOL)
You have a varchar field where they input the number / fraction.
You take it apart and create a decimal.
I would only add a pre IF to look for an existing decimal.
Then plug it into your formula.

The hard part is displaying the result in a fraction.

Simple Example: 3 3/16 x 12
Is 3.1875 x 12 = 38.25

Ok now display 38 25/100

Woops can’t have that, Need to take it back apart and reduce it to 38 1/4 but watch out for 33/100 or 66/100 ext.

Or you build a master conversion table that sets for all combination of 100ths fractions or 1000ths fractions if you want 3rd digit accuracy ext.

In short it’s a mess and not worth the time it takes to build. Remember we are talking about the possibility of doing this to every variable and result in a set of formulas.


I understand what jeff is saying, the computations are to complex.



Jim
Users <> Logic
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-02-12 : 08:12:19
Jeff, I think you might need to borrow that ball bat with the large nails in it - minus the nails.... ouch!
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-02-12 : 08:43:46
quote:
In short it’s a mess and not worth the time it takes to build. Remember we are talking about the possibility of doing this to every variable and result in a set of formulas.......I understand what jeff is saying, the computations are to complex.



Sounds like a good argument why not to do this. But If not given the option, here is a link to a decimal to fraction function which may be of help.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1956&lngWId=10
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-02-12 : 09:03:32
quote:
Originally posted by JimL

Bret actually has the right idea. (As usual) (don’t get a big head LOL)



As if.....

And In that, I always follow the addage, "it's better to give than to receive".

This is one of those times when the absoult (make mine a double, rocks, twist) is strictly followed....





Brett

8-)
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-02-12 : 14:48:27
Decimal to Fraction function.....


--declare some vars
declare @decimal numeric(38,9),
@z numeric(38,9),
@accuracy numeric(38,9),
@prev2Xdenom int,
@numerator int,
@denominator int,
@temporary int,
@end int,
@fraction varchar(22)

--input vars
set @decimal = .333333
set @accuracy = .000000001 --deterines the accuracy level


--Initialize local vars
set @end = 0
set @z = @decimal
set @prev2xdenom = 0
set @denominator = 1

fraction_loop:

set @z = 1/(@z - convert(int,@z))
set @temporary = @denominator
set @denominator = @denominator * convert(int,@z) + @prev2xdenom
set @numerator = round(@decimal*@denominator,0)
set @prev2xdenom = @temporary

if ( ABS(@decimal-(convert(numeric(38,9),@numerator)/convert(numeric(38,9),@denominator))) < @accuracy ) OR ( @z = convert(int,@z) )
begin
--Exit out of loop - assign fraction
set @end = 1
set @fraction = convert(varchar(10),@numerator) + '/' + convert(varchar(10),@denominator)
end
else
begin
goto fraction_loop
end

select @fraction fraction
Go to Top of Page
    Next Page

- Advertisement -