473,325 Members | 2,774 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,325 software developers and data experts.

SQL Server 2005 - SELECT query

Hi

I currently have a select query with "Description = 'input from user
here'" which basically returns the associated row containing the
Description field exactly as typed by the user, however, would it be
possible to, if not found, return the closest match? I am using
Microsoft Visual Studio 2005 with C# as language and Microsoft SQL
Server 2005 as database.

Regards,

Lionel Pinkhard

---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0642-0, 2006/10/17
Tested on: 2006/10/18 12:46:07 PM
avast! - copyright (c) 1988-2006 ALWIL Software.
http://www.avast.com

Oct 18 '06 #1
4 14627
Lionel,

I am making an assumption that you can return multiple rows to the user
to pick from. And that you want the exact match to appear as the most
relevant and first in the list.

SELECT 1 AS sort_order, row_data
FROM tbl
WHERE Description = 'input'
UNION
SELECT 2 AS sort_order, row_data
FROM tbl
WHERE Description like '%input%'
AND Description != 'input'
ORDER BY sort_order, ?

You can use multiple filters and unions to modify and rank the results
ie. WHERE DESCRIPTION like 'input%'
description like substring(input, 1, 10) + '%' --Not sure if this
works outside of a dynamically build query.
Lionel Pinkhard wrote:
Hi

I currently have a select query with "Description = 'input from user
here'" which basically returns the associated row containing the
Description field exactly as typed by the user, however, would it be
possible to, if not found, return the closest match? I am using
Microsoft Visual Studio 2005 with C# as language and Microsoft SQL
Server 2005 as database.

Regards,

Lionel Pinkhard

---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0642-0, 2006/10/17
Tested on: 2006/10/18 12:46:07 PM
avast! - copyright (c) 1988-2006 ALWIL Software.
http://www.avast.com
Oct 18 '06 #2
Lionel,

I am making an assumption that you can return multiple rows to the user

to pick from. And that you want the exact match to appear as the most
relevant and first in the list.
SELECT 1 AS sort_order, row_data
FROM tbl
WHERE Description = 'input'
UNION
SELECT 2 AS sort_order, row_data
FROM tbl
WHERE Description like '%input%'
AND Description != 'input'
ORDER BY sort_order, ?
You can use multiple filters and unions to modify and rank the results
ie. WHERE DESCRIPTION like 'input%'
description like substring(input, 1, 10) + '%' --Not sure if this
works outside of a dynamically build query.

You can also use the CHARINDEX funtion to look for some or all of the
input

WHERE CHARINDEX(substring('input', 1, 100), description, 1) 0
I put the substring in above to suggest that you can match on only part
of the input if you want.

Lionel Pinkhard wrote:
Hi

I currently have a select query with "Description = 'input from user
here'" which basically returns the associated row containing the
Description field exactly as typed by the user, however, would it be
possible to, if not found, return the closest match? I am using
Microsoft Visual Studio 2005 with C# as language and Microsoft SQL
Server 2005 as database.

Regards,

Lionel Pinkhard

---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0642-0, 2006/10/17
Tested on: 2006/10/18 12:46:07 PM
avast! - copyright (c) 1988-2006 ALWIL Software.
http://www.avast.com
Oct 18 '06 #3
Bill wrote:
Lionel Pinkhard wrote:
>I currently have a select query with "Description = 'input from user
here'" which basically returns the associated row containing the
Description field exactly as typed by the user, however, would it be
possible to, if not found, return the closest match? I am using
Microsoft Visual Studio 2005 with C# as language and Microsoft SQL
Server 2005 as database.
I am making an assumption that you can return multiple rows to the user

to pick from. And that you want the exact match to appear as the most
relevant and first in the list.
SELECT 1 AS sort_order, row_data
FROM tbl
WHERE Description = 'input'
UNION
SELECT 2 AS sort_order, row_data
FROM tbl
WHERE Description like '%input%'
AND Description != 'input'
ORDER BY sort_order, ?
[top-posting fixed]

Does this work? I forget whether ORDER BY accepts calculated columns.

select
case when Description = 'input' then 1
when Description like '%input%' then 2
else -1 -- can't happen
end as sort_order,
row_data
from tbl
where Description like '%input%'
order by sort_order -- and optionally one or more additional columns
Oct 18 '06 #4
You can try it - It looks like it could be valid.

If you have not worked out the ORDER BY - tip: Use ORDER BY 1 lest you
have to repeat the case statement within the order by clause

Generally, I stay away from CASE statements whenever there is another
way ( for performance reason )
Ed Murphy wrote:
Bill wrote:
Lionel Pinkhard wrote:
>I currently have a select query with "Description = 'input from user
>here'" which basically returns the associated row containing the
>Description field exactly as typed by the user, however, would it be
>possible to, if not found, return the closest match? I am using
>Microsoft Visual Studio 2005 with C# as language and Microsoft SQL
>Server 2005 as database.
I am making an assumption that you can return multiple rows to the user

to pick from. And that you want the exact match to appear as the most
relevant and first in the list.
SELECT 1 AS sort_order, row_data
FROM tbl
WHERE Description = 'input'
UNION
SELECT 2 AS sort_order, row_data
FROM tbl
WHERE Description like '%input%'
AND Description != 'input'
ORDER BY sort_order, ?

[top-posting fixed]

Does this work? I forget whether ORDER BY accepts calculated columns.

select
case when Description = 'input' then 1
when Description like '%input%' then 2
else -1 -- can't happen
end as sort_order,
row_data
from tbl
where Description like '%input%'
order by sort_order -- and optionally one or more additional columns
Oct 18 '06 #5

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

Similar topics

5
by: christopher.secord | last post by:
Here's a quick and dirty example of a legitimate bug in SQL Server. I've seen other examples, but they were all very complex, some even involving cursors and such. This one will produce the error...
5
by: hishamfangs | last post by:
Hi guys! I'm facing a problem and I can't quite figure it out! I have created a table on SQL Server 2000 to store call records. We get about 250,000 calls a day, and the most frequently used...
9
by: billmiami2 | last post by:
I was playing around with the new SQL 2005 CLR functionality and remembered this discussion that I had with Erland Sommarskog concerning performance of scalar UDFs some time ago (See "Calling...
2
by: Isam | last post by:
Hello, Any help is greatly appreciated. I've recently upgraded my MSDE 2000 to SQL Server 2005 Express Edition on my Windows 2003 server SP1. I've noticted that running a single one ...
18
by: mollyf | last post by:
I just installed SQL Server 2005 on my PC (the developer's edition) yesterday. I have some scripts written by one of my coworkers to create some tables and stored procedures in a database that...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
2
by: Dean g | last post by:
Hi, I'm trying to use the result from the select within the same query. See code SELECT accountnum, char(ASCII(SUBSTRING(accountnum, 1, 1))) as firstChar from questions where firstchar='m' ...
2
by: Dinesh | last post by:
Hi experts, I am working on SQL Server 2005. Now i have to write a query which will extract some information from a table. My main table is having few columns supose 3 columns. EmpID ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.