# is there a way to sort fractions in sql?

I have a series of values like this:

14 1/2
14 1/4
14 5/16
15 15/16
15 7/8

and I need to be able to sort using those values.
LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Do you mean you have strings like those??? If so you'd have to convert them to numbers to be able to sort them.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
no direct way,
you may write a function to evaluate the fractions to decimal equivalent and use that value to sort your results.
0
Author Commented:
ok, that's what I thought but I'm going to leave this question open for a bit to see if someone has another idea.
0
Commented:
It's not really about ideas. It simply cannot be done. Converting it to a number is the only way you can sort it as a number.
0
Commented:
created a quick sample function , not tested for all conditions, you can make a start from here

create the function using the code snippet and use it in your sql as follows

select dbo.ConvertFract(yourFieldName), yourFieldName from tablename order by 1
``````CREATE FUNCTION ConvertFract
(
@Val varchar(10)
)
RETURNS numeric(20,10)
AS
BEGIN
declare @temp varchar(10)
declare @ret numeric(20,10)
declare @frac numeric(20,10)

select @temp = ltrim(rtrim(@val)),@frac=0

if charindex(' ', @temp)<> 0
begin
select @ret = convert(numeric(20,10),SUBSTRING(@temp,1,charindex(' ', @temp)-1))
Select @temp = replace(@temp,SUBSTRING(@temp,1,charindex(' ', @temp)),'')
end

if charindex('/', @val)<> 0
begin
select @frac = convert(numeric(20,10),SUBSTRING(@temp,1,charindex('/', @temp)-1))
Select @temp = replace(@temp,SUBSTRING(@temp,1,charindex('/', @temp)),'')
select @frac = @frac / convert(numeric(20,10),@temp)
end

RETURN @ret + @frac

END
``````
0
Commented:
small modification try this,
``````Alter FUNCTION ConvertFract
(
@Val varchar(10)
)
RETURNS numeric(20,10)
AS
BEGIN
declare @temp varchar(10)
declare @ret numeric(20,10)
declare @frac numeric(20,10)

select @temp = ltrim(rtrim(@val)),@frac=0, @ret = 0

if charindex(' ', @temp)= 0 and charindex('/', @temp)= 0
select @ret = convert(numeric(20,10),@temp)

if charindex(' ', @temp)<> 0
begin
select @ret = convert(numeric(20,10),SUBSTRING(@temp,1,charindex(' ', @temp)-1))
Select @temp = replace(@temp,SUBSTRING(@temp,1,charindex(' ', @temp)),'')
end

if charindex('/', @val)<> 0
begin
select @frac = convert(numeric(20,10),SUBSTRING(@temp,1,charindex('/', @temp)-1))
Select @temp = replace(@temp,SUBSTRING(@temp,1,charindex('/', @temp)),'')
select @frac = @frac / convert(numeric(20,10),@temp)
end

RETURN @ret + @frac

END
``````
0
Author Commented:
I had to split it between the two of your because I think you answered it within seconds of each other. thanks for you help!
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.