PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 13th, 2002, 08:55 PM
a.koepke's Avatar
a.koepke a.koepke is offline
Second highest poster :p
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Jul 2001
Posts: 7,322 a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 8 h 13 m 55 sec
Reputation Power: 31
PostgreSQL Order By

I have been having a problem when PostgreSQL was ordering some data via product name, the product names are as follows:

1. ProductXYZ
2. ProductHJF

...... and so on till

10. Product GHT

The client wanted the page to order them by the number, this worked but alphabetically 10 comes before 1. Due to the data field being varchar PostgreSQL ordered it alphabetically. Now our solution was to either modify the database and our pages that we had coded so far (quite a big task) or develop an SQL query that would rank it numerically. We decided there has to be a way to do this using SQL and we figured it out. If you have the same problem at any time use this.

select * from products order by substring(product_name from 1 for position('.' in product_name)-1)::text::integer

What this does is separate the number from the string, then type casts it as an integer and PostgreSQL sorts it perfectly.
__________________
- Andreas Koepke

Koepke Photography


Reply With Quote
  #2  
Old May 14th, 2002, 04:44 PM
Ted Striker Ted Striker is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 409 Ted Striker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Thumbs up

Thanks for the tip Andreas, I'm sure I will run into that problem sooner or later.

Reply With Quote
  #3  
Old June 6th, 2002, 04:56 PM
niyogi niyogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Austin,TX
Posts: 62 niyogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 55 sec
Reputation Power: 0
very cool method! thanks!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > PostgreSQL Order By

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap