INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Most match result

Most match result

(OP)
I'm searching for a query which search for te most match based on the search value '12345678'
The search value must be start from the first number.


My database has to colums

A |B
------------------------
1 |a
12 |b
123 |c
1234 |d
2345678 |e

What I want is the result of column B where the match is 1234. The result must be 'd' in this case

I hope someone can help me with this query.

RE: Most match result

The most what?
The most similar? In that case you would get 2345678.
The longest substring match? would again be 2345678.
The longest substring mathc beginning from the first character?

I don't quite get what exactly you need.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Most match result

try....

CODE

Declare @Search VarChar(20);
Set @Search = '12345678';

Select Top 1 *
From   YourTableNameHere
Where  @Search like ColumnA + '%'
Order By Len(Id) Desc; 


-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

RE: Most match result

(OP)
@MakeItSo

The first charachter of '12345678' must match with the first character of column a.

So the match can't be '234567'

@gmmastros

Thanks, but not working.




RE: Most match result

(OP)
I have found what I needed


Declare @Search VarChar(20);
Set @Search = '12345678'

SELECT TOP 1
[a]
,[b]
FROM [database]
WHERE a = SUBSTRing(@Search,1,1) or a = SUBSTRing(@Search,1,2) or a = SUBSTRing(@Search,1,3) or a = SUBSTRing(@Search,1,4)
order by a desc

RE: Most match result

I think Georges code just needs a slight change:

CODE -->

Declare @Search VarChar(20);
Set @Search = '12345678';

Select Top 1 *
From   YourTableNameHere
Where  @Search like Rtrim(ColumnA) + '%'
Order By Len(Rtrim(ColumnA)) Desc; 

You fail to even recognize the good idea, your substring conditions only cover cases up to four chars matching, for the general case you would need to continue and it's way more elegant to simply just rtrim.

Bye, Olaf.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close