473,516 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting back set order from the IN param

Hi All

My query is as follows:

SELECT STRINGTEXT, TOKENID
FROM WEBSTRINGS
WHERE TOKENID IN (6,20,234,19,32,4,800,177)

All I want is my resultset to come back in the order that I have defined in
the IN clause, but unfortunately SQL is trying to be too helpful and sorts
the numbers in the IN clause so that the resultset comes back with a TOKENID
order of 4,6,19,20,32,177,234,800.

I don't want this bloody order I want 6,20,234,19,32,4,800,177!!

Sorry for my rant, but its got my hot under the collar.

Is there anyway round this?

Thanks

Yobbo


Nov 5 '06 #1
3 1707
In SQL, you *must* specify ORDER BY to return data in a particular sequence.
Consider results to be unordered unless you explicitly specify ORDER BY in
the outermost query. Even if a query returned results in your desired order
without ORDER BY, it would be due to chance rather than designed behavior.

Below is one method to accomplish your task.

SELECT STRINGTEXT, TOKENID
FROM WEBSTRINGS
JOIN (
SELECT 6 AS TOKENID, 1 AS SEQ
UNION ALL SELECT 20, 2
UNION ALL SELECT 234, 3
UNION ALL SELECT 19, 4
UNION ALL SELECT 32, 5
UNION ALL SELECT 4, 6
UNION ALL SELECT 800, 7
UNION ALL SELECT 177, 8) AS SELECT_SEQUENCE ON
SELECT_SEQUENCE.TOKENID = WEBSTRINGS.TOKENID
ORDER BY SELECT_SEQUENCE.SEQ
--
Hope this helps.

Dan Guzman
SQL Server MVP

"Yobbo" <in**@SpamMeNot.co.ukwrote in message
news:12*************@corp.supernews.com...
Hi All

My query is as follows:

SELECT STRINGTEXT, TOKENID
FROM WEBSTRINGS
WHERE TOKENID IN (6,20,234,19,32,4,800,177)

All I want is my resultset to come back in the order that I have defined
in
the IN clause, but unfortunately SQL is trying to be too helpful and sorts
the numbers in the IN clause so that the resultset comes back with a
TOKENID
order of 4,6,19,20,32,177,234,800.

I don't want this bloody order I want 6,20,234,19,32,4,800,177!!

Sorry for my rant, but its got my hot under the collar.

Is there anyway round this?

Thanks

Yobbo

Nov 5 '06 #2
Yobbo (in**@SpamMeNot.co.uk) writes:
My query is as follows:

SELECT STRINGTEXT, TOKENID
FROM WEBSTRINGS
WHERE TOKENID IN (6,20,234,19,32,4,800,177)

All I want is my resultset to come back in the order that I have defined
in the IN clause, but unfortunately SQL is trying to be too helpful and
sorts the numbers in the IN clause so that the resultset comes back with
a TOKENID order of 4,6,19,20,32,177,234,800.

I don't want this bloody order I want 6,20,234,19,32,4,800,177!!

Sorry for my rant, but its got my hot under the collar.
Actually, SQL Server does not sort at all. It just retrieves the rows
in the order which happens to be most efficient.

As an alterantive to Dan's suggestion, this may or may not fit better
to your actual problem:

SELECT STRINGTEXT, TOKENID
FROM WEBSTRINGS
WHERE TOKENID IN (6,20,234,19,32,4,800,177)
ORDER BY CASE TOKENID
WHEN 6 THEN 1
WHEN 20 THEN 2
WHEN 234 THEN 3
-- etc
END

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 5 '06 #3
Many thanks Dan

Rgds Yobbo
"Dan Guzman" <gu******@nospam-online.sbcglobal.netwrote in message
news:co****************@newssvr27.news.prodigy.net ...
In SQL, you *must* specify ORDER BY to return data in a particular sequence.
Consider results to be unordered unless you explicitly specify ORDER BY in
the outermost query. Even if a query returned results in your desired order
without ORDER BY, it would be due to chance rather than designed behavior.

Below is one method to accomplish your task.

SELECT STRINGTEXT, TOKENID
FROM WEBSTRINGS
JOIN (
SELECT 6 AS TOKENID, 1 AS SEQ
UNION ALL SELECT 20, 2
UNION ALL SELECT 234, 3
UNION ALL SELECT 19, 4
UNION ALL SELECT 32, 5
UNION ALL SELECT 4, 6
UNION ALL SELECT 800, 7
UNION ALL SELECT 177, 8) AS SELECT_SEQUENCE ON
SELECT_SEQUENCE.TOKENID = WEBSTRINGS.TOKENID
ORDER BY SELECT_SEQUENCE.SEQ
--
Hope this helps.

Dan Guzman
SQL Server MVP

"Yobbo" <in**@SpamMeNot.co.ukwrote in message
news:12*************@corp.supernews.com...
Hi All

My query is as follows:

SELECT STRINGTEXT, TOKENID
FROM WEBSTRINGS
WHERE TOKENID IN (6,20,234,19,32,4,800,177)

All I want is my resultset to come back in the order that I have defined
in
the IN clause, but unfortunately SQL is trying to be too helpful and sorts
the numbers in the IN clause so that the resultset comes back with a
TOKENID
order of 4,6,19,20,32,177,234,800.

I don't want this bloody order I want 6,20,234,19,32,4,800,177!!

Sorry for my rant, but its got my hot under the collar.

Is there anyway round this?

Thanks

Yobbo


Nov 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5736
by: Mavis | last post by:
Hi, I'm trying to use a simple form to select by which column I will display output from a MySQL database. I'd like to display based on ORDER BY and select Written, Average, etc. I know the data is getting to the script from the post, I just can't get it to the select statement.
1
1334
by: Jim Geissman | last post by:
I have a function that returns a table: CREATE FUNCTION dbo.Example(@Param int) RETURNS @Tbl TABLE ( Field1 int, Field2 int) AS BEGIN INSERT @Tbl (Field1,Field2) SELECT FieldA,FieldB FROM DataTable WHERE FieldC = @Param
2
6890
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on a type mismatch. It is positively because of the boolean(java primitive)parameter. It goes fine if I change this parameter to int or String. This...
3
1691
by: for.fun | last post by:
Hi everybody, I am looking for a XML comparison tool (I do not mean a standard char-by-char diff tool but a tool which understand XML syntax) More precisely, I can have serveral XML structures organized differently. The XML nodes can store the same data but be organized differently => in such a case, I would like the diff tool to tell...
10
1917
by: Peter Afonin | last post by:
Hello, I have a simple client-side form that is checking the domain availability on the domain registrar's server: <FORM action="https://www.webnames.ru/scripts/RegTimeSRS.pl" method="post"> <input type="hidden" name="thisPage" value="pispCheckDomain"> <input type="hidden" name="username" value="test"> <input type="hidden"...
4
4251
by: Yobbo | last post by:
Hi All My query is as follows: SELECT STRINGTEXT, TOKENID FROM WEBSTRINGS WHERE TOKENID IN (6,20,234,19,32,4,800,177) All I want is my resultset to come back in the order that I have defined in the IN clause, but unfortunately SQL is trying to be too helpful and sorts
5
2182
by: noLoveLusT | last post by:
hi everyone i am very very new to the sql server (2 days actually and ) so far i learned creating SPs etc but couldnt workout how to get return value from my prodecure my sp as follows ________________________ CREATE PROCEDURE . @PageIndex INT, @PageSize INT, @Total INT OUTPUT
0
1487
by: JohnP | last post by:
Hi all Does anyone know the best way to get feedback from a Windows Service? I have written a Windows service which uses a class library (a DLL) which has a timer which does some operations every <xseconds. I'd like to be able to somehow get the current 'elapsed' seconds of the timer, with a view to displaying something on the service...
3
1922
by: TC | last post by:
Hey All, I have some classes that I recently built for a website which uses the HttpWebRequest & HttpWebResponse objects from the System.Net namespace. Basically, the classes rap submitted data up, connect to external websites on external servers and post / remove the data from these other sites. It works fine locally but when uploaded to...
7
4691
by: Andrus | last post by:
How to get syntactically correct signature which compiles for code template grneration ? I tried code below but it creates syntactically incorrect signature. Andrus. using System; using System.Collections.Generic; public class MainClass
0
7276
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7581
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7548
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3267
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1624
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.