Unix Technical Forum

numeric sorting on a char field having a decimal

This is a discussion on numeric sorting on a char field having a decimal within the Informix forums, part of the Database Server Software category; --> Hi, In Informix, I have a char(6) column in which decimals are stored. I need to sort in the ...


Go Back   Unix Technical Forum > Database Server Software > Informix

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-20-2008, 11:24 AM
Jaini
 
Posts: n/a
Default numeric sorting on a char field having a decimal

Hi,

In Informix, I have a char(6) column in which decimals are stored. I
need to sort in the numeric order. I have seen couple of query posted
before which deals with numeric stored in a char field but I'm looking
for decimals stored in the char.

I need the o/p in following format -

10.1
10.2
10.3
10.10
10.22
10.29
10.4

Thanks & Regards
Abhinav

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-20-2008, 11:24 AM
Obnoxio The Clown
 
Posts: n/a
Default Re: numeric sorting on a char field having a decimal


Jaini said:
>
> Hi,
>
> In Informix, I have a char(6) column in which decimals are stored. I
> need to sort in the numeric order. I have seen couple of query posted
> before which deals with numeric stored in a char field but I'm looking
> for decimals stored in the char.
>
> I need the o/p in following format -
>
> 10.1
> 10.2
> 10.3
> 10.10


Actually, 10.10 = 10.1

> 10.22
> 10.29
> 10.4


If you have IDS 9, you can cast them to DECIMAL. If not, you can try
adding 0.00 to each.

SELECT col:ECIMAL FROM tab ORDER BY 1

or

SELECT col + 0.00 FROM tab ORDER BY 1

Neither option has been tested. )

--
Bye now,
Obnoxio

"C'est pas parce qu'on n'a rien à dire qu'il faut fermer sa gueule"
- Coluche

did i mention i like nulls? heck, i even go so far as to say that all
columns in a table except the primary key could/should be nullable. this
has certain advantages, for example, if you need to insert a child record
and you don't have a parent row for it, just do an insert into the parent
table with the primary key value (everything else null), and voila,
relational integrity is preserved. but this is, admittedly, a bit
controversial among modellers.

--r937, dbforums.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-20-2008, 11:24 AM
Richard Harnden
 
Posts: n/a
Default Re: numeric sorting on a char field having a decimal

Jaini wrote:
> Hi,
>
> In Informix, I have a char(6) column in which decimals are stored. I
> need to sort in the numeric order. I have seen couple of query posted
> before which deals with numeric stored in a char field but I'm looking
> for decimals stored in the char.
>
> I need the o/p in following format -
>
> 10.1
> 10.2
> 10.3
> 10.10
> 10.22
> 10.29
> 10.4
>


You don't say which version you have.

For 7.x, you SELECT char_column + 0 ... ORDER BY 1

For 9.x, you do as for 7.x or SELECT char_column:ECIMAL ... ORDER BY 1

You'd be better off fixing the table though.

--
rh


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-20-2008, 11:24 AM
scottishpoet
 
Posts: n/a
Default Re: numeric sorting on a char field having a decimal

if you want these sorted in that order then these are not decimals so
you are unlikely to

10.22 is a smaller number than 10.3 and bugg er

ten point twenty two is not a decimal number and may well be bigger
than ten point three

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-20-2008, 11:24 AM
Jaini
 
Posts: n/a
Default Re: numeric sorting on a char field having a decimal

Obnoxio,

It didn't help me as, the in the result the deciamls are padded with
ZERO's and there is no difference between 10.4 & 10.40. I want the
result in following format -

10.1
10.4
10.11
10.40
10.45

But casting or adding 0.00 is resulting

(expression)
10.10000000
10.11000000
10.40000000
10.40000000
10.45000000

Kindly suggest other way.

Thanks for your time.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-20-2008, 11:24 AM
Obnoxio The Clown
 
Posts: n/a
Default Re: numeric sorting on a char field having a decimal


Jaini said:
>
> Obnoxio,
>
> It didn't help me as, the in the result the deciamls are padded with
> ZERO's and there is no difference between 10.4 & 10.40. I want the
> result in following format -
>
> 10.1
> 10.4
> 10.11
> 10.40
> 10.45
>
> But casting or adding 0.00 is resulting
>
> (expression)
> 10.10000000
> 10.11000000
> 10.40000000
> 10.40000000
> 10.45000000


Well, yes, in a decimal number, 10.4 = 10.40. So what you are saying is
that while it contains a decimal point, the number is not, in fact, a
decimal at all.

So what are you actually trying to do?

--
Bye now,
Obnoxio

"C'est pas parce qu'on n'a rien à dire qu'il faut fermer sa gueule"
- Coluche

did i mention i like nulls? heck, i even go so far as to say that all
columns in a table except the primary key could/should be nullable. this
has certain advantages, for example, if you need to insert a child record
and you don't have a parent row for it, just do an insert into the parent
table with the primary key value (everything else null), and voila,
relational integrity is preserved. but this is, admittedly, a bit
controversial among modellers.

--r937, dbforums.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-20-2008, 11:24 AM
Marco Greco
 
Posts: n/a
Default Re: numeric sorting on a char field having a decimal

Jaini wrote:
> Hi,
>
> In Informix, I have a char(6) column in which decimals are stored. I
> need to sort in the numeric order. I have seen couple of query posted
> before which deals with numeric stored in a char field but I'm looking
> for decimals stored in the char.
>
> I need the o/p in following format -
>
> 10.1
> 10.2
> 10.3
> 10.10
> 10.22
> 10.29
> 10.4
>
> Thanks & Regards
> Abhinav


you do realize that the order that you want is neither in char order nor
numeric order, right? in neither 10.10 would be bigger than 10.3

in 9.4 you can order by expressions not in the select list, eg

select c from t order by c+0, length(c)

yields

10.1
10.10
10.11
10.2
10.22
10.29
10.3
10.4

just write an SP that sorts stuff by whatever warped order you like

--
Ciao,
Marco
__________________________________________________ ____________________________
Marco Greco /UK /IBM Standard disclaimers apply!

Structured Query Scripting Language http://www.4glworks.com/sqsl.htm
4glworks http://www.4glworks.com
Informix on Linux http://www.4glworks.com/ifmxlinux.htm

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-20-2008, 11:24 AM
scottishpoet
 
Posts: n/a
Default Re: numeric sorting on a char field having a decimal

10.four is smaller than 10.forty in this alternative decimal notation

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-20-2008, 11:24 AM
Jaini
 
Posts: n/a
Default Re: numeric sorting on a char field having a decimal

Obnoxio,

I agree with you. This is what I'm looking for -

I need to sort the numbers considering only after decimals so that 3
comes before 22 in 10.3 & 10.22.

Thanks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-20-2008, 11:24 AM
Obnoxio The Clown
 
Posts: n/a
Default Re: numeric sorting on a char field having a decimal


Jaini said:
>
> Obnoxio,
>
> I agree with you. This is what I'm looking for -
>
> I need to sort the numbers considering only after decimals so that 3
> comes before 22 in 10.3 & 10.22.


Pffft.

Something like

SELECT col, TRUNC(col+0), (col+0 - TRUNC(col+0))
FROM tab
ORDER BY 2, 3

?

--
Bye now,
Obnoxio

"C'est pas parce qu'on n'a rien à dire qu'il faut fermer sa gueule"
- Coluche

did i mention i like nulls? heck, i even go so far as to say that all
columns in a table except the primary key could/should be nullable. this
has certain advantages, for example, if you need to insert a child record
and you don't have a parent row for it, just do an insert into the parent
table with the primary key value (everything else null), and voila,
relational integrity is preserved. but this is, admittedly, a bit
controversial among modellers.

--r937, dbforums.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:04 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com