Re: sorting of a price field

From: Jay Riddle <jcriddle4(at)yahoo(dot)com>
To: Anne Wainwright <anotheranne(at)fables(dot)co(dot)za>, pgsql-novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: sorting of a price field
Date: 2013-09-14 14:54:27
Message-ID: 1379170467.79371.YahooMailNeo@web126106.mail.ne1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

The shop_price column is probably defined as a text instead of a numeric. You can 
convert the field to a number and then sort _however_ if you have any non-numeric data 
in the field you may get a conversion error. Below is what the query might look like. I was 
going to try and suggest a nicer function than to_number(...) that would handle bad data 
but when I googled for it I found that you have to use regular expressions or write your 
own function. Writing your own six or seven line function is very workable but it would 
be cool to have already built in functions to handle it. If you need to handle bad data 
you might try googling for "postgresql safely convert to number".

SELECT to_number(shop_price) AS ShopPrice
FROM YourTableThatIsNotDefinedWellAsNumberFieldsShouldBeNumeric
ORDER BY to_number(shop_price)

________________________________
From: Anne Wainwright <anotheranne(at)fables(dot)co(dot)za>
To: pgsql-novice <pgsql-novice(at)postgresql(dot)org>
Sent: Saturday, September 14, 2013 5:07 AM
Subject: [NOVICE] sorting of a price field

Hi,

I run a fairly simple query to show current records of stock using the
phrase 'sort by shop_price'.

Surprise (but really no surprise) this is sorted in ascii order rather
than shop_price order. So 110.00 comes before 20.00

I can't see any reference to changing this. What can I do, is this
perhaps to do with the field type, can it be changed?

Thanks
Anne

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Anne Wainwright 2013-09-15 12:25:24 Re: sorting of a price field
Previous Message Kartik Vashishta 2013-09-14 12:47:06 undefined symbol: boot_DynaLoader