Consider this SQL:
SELECT my_field FROM my_table WHERE my_field IN ('value2', 'value1',
'value3')
Simple enough, but is there anyway to specify that the result should be
ordered exactly like the "IN" clause states? So when this recordset
comes back, I want it like this:
my_field
------------
value2
value1
value3
Possible?
Deane 5 11138
(de**********@g mail.com) writes:
Consider this SQL:
SELECT my_field FROM my_table WHERE my_field IN ('value2', 'value1',
'value3')
Simple enough, but is there anyway to specify that the result should be
ordered exactly like the "IN" clause states? So when this recordset
comes back, I want it like this:
my_field
------------
value2
value1
value3
No. The IN clause is just a syntactic shortcut for a bunch of OR operators.
You will need to add explicit ordering, for instance:
ORDER BY CASE my_field WHEN 'value2' THEN 1
WHEN 'value1' THEN 2
WHEN 'value3' THEN 3
END
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.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
Thanks.
FYI -- I've learned in the meantime that MySQL has this functionality:
ORDER BY FIELD(my_field, 'value2','value 1','value3')
Syntactic sugar, to be sure, but still handy.
Deane
Erland Sommarskog wrote:
(de**********@g mail.com) writes:
Consider this SQL:
SELECT my_field FROM my_table WHERE my_field IN ('value2', 'value1',
'value3')
Simple enough, but is there anyway to specify that the result should be
ordered exactly like the "IN" clause states? So when this recordset
comes back, I want it like this:
my_field
------------
value2
value1
value3
No. The IN clause is just a syntactic shortcut for a bunch of OR operators.
You will need to add explicit ordering, for instance:
ORDER BY CASE my_field WHEN 'value2' THEN 1
WHEN 'value1' THEN 2
WHEN 'value3' THEN 3
END
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.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
Deane,
instead of
SELECT my_field FROM my_table WHERE my_field IN ('value2', 'value1',
'value3')
try this:
SELECT my_field FROM my_table
join (
select 'value2' c, 1 n
union all
select 'value1', 2
union all
select 'value3', 3
) t
on my_field = t.c
order by t.n de**********@gm ail.com wrote:
Thanks.
FYI -- I've learned in the meantime that MySQL has this functionality:
ORDER BY FIELD(my_field, 'value2','value 1','value3')
Syntactic sugar, to be sure, but still handy.
The densest way to write it in ANSI SQL is this:
SELECT my_field FROM my_table JOIN (VALUES(1, 'value2'),
(2, 'value1'),
(3, 'value3')) AS V(I, val)
ON myfield = val
ORDER BY i
This way you don't need to repeat the values.
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
IOD Conference http://www.ibm.com/software/data/ond...ness/conf2006/
Serge,
What are you doing here? Are you porting from SQL Server to DB2 these
days? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: kinne |
last post by:
The following code is supposed to reverse the date in "yyyy-mm-dd" format,
but it produces different results in Firefox 1.0 and in Internet Explorer
6SP1. In Firefox, the result is correct ("2004-11-29") but it's wrong in
Internet Explorer 6SP1 ("00:20:15-11-29"). If I change "dateParts" to
"dateParts", it's exactly the opposite that occures: a correct result in
IExplorer but a fault in Firefox. Is there a workaround? Where do I miss the...
|
by: Dmitry Jouravlev |
last post by:
Hi,
I have a number of C++ solutions in Visual Studio .NET and when i compile them using "Whole Program Optimization", certain projects report a LNK1171 error saying that c2.dll could not be loaded. The error contains the correct path to c2.dll (and it is definately there).
This only happens on some projects and only when "whole program optimization" option is turned on. If i turn off this option, the problem goes away. I have other...
|
by: James Jensen |
last post by:
I ran into a snippet of code that uses a FOR EACH loop to step through
the rows in a Dataset.Table object. I understand that the FOR EACH
statement implements the MoveNext function of the IEnumerator
interface of the InternalDataCollectionBase object.
I was wondering if anyone knew whether or not the iterator in this
case uses a certain hashing algorithm? The table object in question
is filled via a stored procedure that returns its...
|
by: google |
last post by:
I'm using ADODB to connect to an Access database from VB6. I'm running
a query that's returning strange results- the following query returns
different results in VB & Access:
SELECT * FROM Robes WHERE strSize NOT LIKE '*+';
I've also run the following three queries in VB with the record count
indicated beneath. Access seems to handle the queries fine, but VB
seems to be having problems with the WHERE clause.
|
by: vivekian |
last post by:
Hi ,
I need to place the results of two different queries in the same result
table parallel to each other.
So if the result of the first query is
1 12
2 34
3 45
| |
by: whiskers |
last post by:
I'm debugging some code and I have to admit that I don't know yet how
it works.
But I ran into a problem I can't explain
The program is a DLL that retrieves raw data from a camera, builds
histograms based on the pixel values, and displays them on the screen;
here's a small excerpt:
VOID CHistogramContainer::appendFrameData(WORD *pdwRawData, int nWidth,
int nHeight)
{
|
by: Mike Will via WebmasterKB.com |
last post by:
I've been struggling with this for a while now. But I want to display my
query results in a table where they are grouped by project. I am using a do
while loop but it not achieving the look I want.
This is what I got:
Project Employee Name OT Hours
Project 1 Employee A 7
Project 1 Employee C 5
Project 1 Employee D 3
Project 2 Employee A 8
|
by: =?Utf-8?B?TGVvbkc=?= |
last post by:
I'm trying to upload a file using an asp:FileUpload control and an
asp:LinkButton.
This works fine, while there is an actual filepath in the fileupload upload
control. Even an empty field works. But a bogus filename results in the
"Access Denied" javascript error in IE7.
This also happens when I use an asp:Button with the UseSubmitBehavior
property set to false. When the UseSubmitBehavior is set to true (default),
the button does...
|
by: 848lu |
last post by:
hi, i am trying to do a search with my ASP webpage, and then display the results in a DIV tag of HTML, how would i do that, thanks
strconnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("shopping.mdb"))
strconnection.Open()
'after creating strings where clause will look up what values the strings holds
sql = "SELECT * FROM Customer_details WHERE Email = @Email"...
|
by: dougmeece |
last post by:
Good day everyone,
I have a database with 2 main forms. The first form is used to add records to the database and contains a command button that opens the 2nd form for records searching.
On the second form I have tow combo boxes that I would like to search from. Currently, I just have the search button run a query which opens in a separate screen. This is the same for both combo boxes (they are independent of each other). I would like...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |