Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 25th, 2005, 09:18 PM
SoC SoC is offline
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default Order a recordset by specific values

Hi,

I was wondering if it is possible to order a recordset by specific values.

That is, I want my recordset to be ordered alphabetically, but I want to set the order of the alphabet.

For example, I don't want my recordset to be odered by A, B, C, D ...I want it to be ordered by G, V, T, A ... or something.

Can you do something like

Code:
SELECT * FROM myTable ORDER BY myField (G, V, T, A, B, C, Q, X)...
etc

Thanks

s

  #2 (permalink)  
Old August 25th, 2005, 11:12 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Create a Table that holds the order you want:

CREATE TABLE [dbo].[MyOrder] (
    [MyField] [char] (1)
    [Order] [int]

Then insert your order data:
    T 3
    A 4
    B 5
    C 6
    G 1
    V 2
    Q 7
    X 8

Then join the 2 tables and order by the order column.
select mt.Myfield
from myTable mt
   inner join MyOrder mo ON mt.MyField = mo.MyField
order by mo.[order]




  #3 (permalink)  
Old August 28th, 2005, 08:27 PM
SoC SoC is offline
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply jbenson001. I ended up creating another column in the table and using that to order the results. It seems to work ok in this instance, but I'll keep your method in mind for future reference.



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting fields, specific order from xml using xslt Jaipal XSLT 4 July 23rd, 2007 11:35 AM
sort recordset in different order pablohoney Classic ASP Databases 0 December 29th, 2005 08:32 PM
using a sequence to order a recordset Adam H-W Classic ASP Databases 1 August 21st, 2005 11:12 PM
View recordset for the specific user mikersantiago Classic ASP Basics 4 October 20th, 2004 10:19 AM
Selection of a Specific Recordset gmoney060 Classic ASP Basics 1 August 9th, 2004 03:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.