If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Ignore Prefix

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-11-12, 10:25
Erwina Erwina is offline
Registered User
 
Join Date: Jan 2012
Posts: 8
Ignore Prefix

Morning!

This is more of a "how can this be better" question, as to my knowledge the query below functions as expected.

Overall, I am trying to make a query (TSQL allowed) that takes a user input value (@Input) and sees if it exists in dbo.Table, ignoring prefix characters.

e.g. T.Value = FOOBAR
@Input = FOOBAR (true)
@Input = AFOOBAR (true)
@Input = FOORBARA (false)
@Input = FFOOBAR (true)
@Input = FOOBARR(false)
@Input = F (false)
etc..

Fairly confident the below works, however, I wanted to get other opinions as I am curious if a better/more efficient solution exists.

SELECT *
FROM dbo.Table AS T
WHERE CHARINDEX(REVERSE(T.Value), @Input, 0) > 0
AND @Input LIKE REVERSE(T.Value) + '%'

Thanks,
Reply With Quote
  #2 (permalink)  
Old 01-11-12, 11:44
MCrowley MCrowley is offline
Wage drone 24601
 
Join Date: Jan 2003
Location: Massachusetts
Posts: 4,899
What is the CHARINDEX used for in the query above?
Reply With Quote
  #3 (permalink)  
Old 01-11-12, 11:56
Erwina Erwina is offline
Registered User
 
Join Date: Jan 2012
Posts: 8
That is actually a good point.. I since added the LIKE check to fix a case that I found out about, and apparently that rendered the CHARINDEX condition useless as well.

Just using

SELECT *
FROM dbo.Table AS T
WHERE @Input LIKE REVERSE(T.Value) + '%'

Seems to still give all the results I expect.. Though regardless is using REVERSE() and LIKE a acceptable route to take or is there something more efficient out there?
Reply With Quote
  #4 (permalink)  
Old 01-11-12, 11:58
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
How do you know what PART of @Input you want to look at?
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #5 (permalink)  
Old 01-11-12, 12:12
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Colour me confused.
Why the reverse anyways?
Code:
WHERE  t.value LIKE '%' + @input
__________________
George
Twitter | Blog
Reply With Quote
  #6 (permalink)  
Old 01-11-12, 13:15
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
I'm assuming the leading values in the variable


How do you know there are no trailing values (suffix I guess)

How do you prevent that?

Is it always only 1 prefix char?
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On