473,382 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

DataReader + SQL + sortorder

Curious little problem...

Suppose I have these fields in my table ("NameTable"):

FirstName LastName SearchTerms

Kathleen Smith Smith Kathleen
Gladys Lee Lee Gladys
Lee Burton Oswald Lee

I'm searching for the string "lee" wherever it may appear.

My SQL is

"SELECT FROM NameTable.* WHERE SearchTerms Like '%lee%'"

I want to order my results as follows:

1. People with the last name of "Lee" are first;
2. People with the first name of "Lee" are second;
3. People having a "lee" in their name are third.

Unless I'm missing something, a SQL UNION statement doesn't seem to produce
the power to sort the results in this order.

Is it possible to run three separate SQL statements and stick the results
into a single datareader (using nextresult()), which I can then output to
the datagrid?

Thanks for any help!

--Brent
Nov 18 '05 #1
3 1011

"Brent" <bbigler @ ahem-'whoopee' . com> wrote in message
news:2A*****************@newssvr27.news.prodigy.co m...
Curious little problem...

Suppose I have these fields in my table ("NameTable"):

FirstName LastName SearchTerms

Kathleen Smith Smith Kathleen
Gladys Lee Lee Gladys
Lee Burton Oswald Lee

I'm searching for the string "lee" wherever it may appear.

My SQL is

"SELECT FROM NameTable.* WHERE SearchTerms Like '%lee%'"


SELECT * FROM NameTable
WHERE SearchTerms Like '%lee%'
ORDER BY CASE WHEN SearchTerms Like '% Lee' then 1
WHEN SearchTerms Like 'Lee %' then 2
ELSE 3 END
Davie
Nov 18 '05 #2
Try reposting this in microsoft.public.sqlserver.programming, if it can be done with sql, someone will know there.

"Brent" <bbigler @ ahem-'whoopee' . com> wrote in message news:2A*****************@newssvr27.news.prodigy.co m...
Curious little problem...

Suppose I have these fields in my table ("NameTable"):

FirstName LastName SearchTerms

Kathleen Smith Smith Kathleen
Gladys Lee Lee Gladys
Lee Burton Oswald Lee

I'm searching for the string "lee" wherever it may appear.

My SQL is

"SELECT FROM NameTable.* WHERE SearchTerms Like '%lee%'"

I want to order my results as follows:

1. People with the last name of "Lee" are first;
2. People with the first name of "Lee" are second;
3. People having a "lee" in their name are third.

Unless I'm missing something, a SQL UNION statement doesn't seem to produce
the power to sort the results in this order.

Is it possible to run three separate SQL statements and stick the results
into a single datareader (using nextresult()), which I can then output to
the datagrid?

Thanks for any help!

--Brent

Nov 18 '05 #3
Thanks, Davie! That worked like a charm!

--B
"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:uO**************@tk2msftngp13.phx.gbl...

"Brent" <bbigler @ ahem-'whoopee' . com> wrote in message
news:2A*****************@newssvr27.news.prodigy.co m...
Curious little problem...

Suppose I have these fields in my table ("NameTable"):

FirstName LastName SearchTerms

Kathleen Smith Smith Kathleen
Gladys Lee Lee Gladys
Lee Burton Oswald Lee

I'm searching for the string "lee" wherever it may appear.

My SQL is

"SELECT FROM NameTable.* WHERE SearchTerms Like '%lee%'"


SELECT * FROM NameTable
WHERE SearchTerms Like '%lee%'
ORDER BY CASE WHEN SearchTerms Like '% Lee' then 1
WHEN SearchTerms Like 'Lee %' then 2
ELSE 3 END
Davie

Nov 18 '05 #4

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

Similar topics

6
by: Yasutaka Ito | last post by:
Hi, My friend had a little confusion about the working of DataReader after reading an article from MSDN. Following is a message from him... <!-- Message starts --> I was going thru DataReader...
6
by: Ravi | last post by:
Hi, I am not able to understand why a datareader needs a connection to the DB all the time. Here is what I tried. Sqlcommand cmd = ("select * from table1",con) // where con is the connection...
5
by: Jason Huang | last post by:
Hi, Is it possible to bind DataReader to a DataGrid in C# windows form? And how? And can we update data in a DataSet by using the DataReader? Thanks for help. Jason
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
2
by: Andrei Pociu | last post by:
In a typical ASP .NET Web Application (website), I'm currently using a class where I declare some public static objects. For example there's the place where I initialize the SqlConnection. Also...
1
by: Brent | last post by:
I'm having a hard time wrapping my head around how to build a multi-dimensional array of n length out of a DataReader loop. Take this pseudo-code: ======================================= public...
7
by: Varangian | last post by:
Hi all, the question I want to ask if the conversion of a DataReader to a Table looping through the DataReader is better than using the Fill Method of the DataAdapter... I'm asking because...
7
by: Diffident | last post by:
Hello All, I would like to use DataReader based accessing in my Data Access Layer (DAL). What is considered to be a best practice while returning from a DAL method that executes a query and...
3
by: Johnny Jörgensen | last post by:
I've got an error that I simply cannot locate: I've got a form in which I use a datareader object to read information from a db. After the read, I close the reader and I dispose of both the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.