dbTalk Databases Forums  

SQL Like.

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL Like. in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dishan Fernando
 
Posts: n/a

Default SQL Like. - 04-05-2005 , 11:04 PM






Hi,
I want to search Strings Like this.
Say Column containing ...
"A B"
"A B"
"A B"
"A B"
"A B"

How can I search "A B" with ignoring spaces. Result must return all the
records.

Thank you.
Dishan


Reply With Quote
  #2  
Old   
Madhivanan
 
Posts: n/a

Default Re: SQL Like. - 04-06-2005 , 01:35 AM







Try this

Select column from table where left(column,1)='A' and
Right(column,1)='B'

Madhivanan


Reply With Quote
  #3  
Old   
Dishan Fernando
 
Posts: n/a

Default Re: SQL Like. - 04-06-2005 , 01:55 AM



No. I just want to search "A B". but it must be return all values that
ignoring space at the middle.


Reply With Quote
  #4  
Old   
Steve Jorgensen
 
Posts: n/a

Default Re: SQL Like. - 04-06-2005 , 03:21 AM



On 5 Apr 2005 23:55:56 -0700, "Dishan Fernando" <dishan (AT) gmail (DOT) com> wrote:

Quote:
No. I just want to search "A B". but it must be return all values that
ignoring space at the middle.
Any way you might do it will be inefficient. Essentially, you have to write a
custom function that collapses strings of multiple spaces, and call it from
within the query, or open a recordset, loop through it, and call the compare
function for each row. Calling the function from within the query is simpler,
but looping through the recordset is more robust, and probably at least as
fast.

Here's one way you might write the function (not tested)...

Public Function CollapseSpaces(Value As String) As String
Dim strResult As String

strResult = Trim$(Value)
Do While Instr(strResult, " ") > 0
strResult = Replace(strResult, " ", " ")
Loop

CollapseSpaces = strResult
End Function


Reply With Quote
  #5  
Old   
Steve Jorgensen
 
Posts: n/a

Default Re: SQL Like. - 04-06-2005 , 03:25 AM



Oops - I thought I was reading the MS Access group. I'm not sure how you do
this in SQL Server, but it's probably a similar solution in TSQL to the VB/VBA
code I suggested.

On Wed, 06 Apr 2005 01:21:59 -0700, Steve Jorgensen <nospam (AT) nospam (DOT) nospam>
wrote:

Quote:
On 5 Apr 2005 23:55:56 -0700, "Dishan Fernando" <dishan (AT) gmail (DOT) com> wrote:

No. I just want to search "A B". but it must be return all values that
ignoring space at the middle.

Any way you might do it will be inefficient. Essentially, you have to write a
custom function that collapses strings of multiple spaces, and call it from
within the query, or open a recordset, loop through it, and call the compare
function for each row. Calling the function from within the query is simpler,
but looping through the recordset is more robust, and probably at least as
fast.

Here's one way you might write the function (not tested)...

Public Function CollapseSpaces(Value As String) As String
Dim strResult As String

strResult = Trim$(Value)
Do While Instr(strResult, " ") > 0
strResult = Replace(strResult, " ", " ")
Loop

CollapseSpaces = strResult
End Function


Reply With Quote
  #6  
Old   
Dishan Fernando
 
Posts: n/a

Default Re: SQL Like. - 04-06-2005 , 03:50 AM




select *
FROM Table1
WHERE REPLACE(COL,' ','') LIKE '%A B%' AND
Col NOT LIKE '%AB%'


Reply With Quote
  #7  
Old   
Steve Jorgensen
 
Posts: n/a

Default Re: SQL Like. - 04-06-2005 , 04:01 AM



On 6 Apr 2005 01:50:14 -0700, "Dishan Fernando" <dishan (AT) gmail (DOT) com> wrote:

Quote:
select *
FROM Table1
WHERE REPLACE(COL,' ','') LIKE '%A B%' AND
Col NOT LIKE '%AB%'
I thought you needed the code to work when supplied with just 'A B' as the
criteria. I also thought you did not want to match, for instance 'XAB'.


Reply With Quote
  #8  
Old   
Sandy
 
Posts: n/a

Default Re: SQL Like. - 04-06-2005 , 07:46 AM



The answer is in your question:

Use the 'Like' command.

Select X, y, z
From TableA
Where FieldA Like 'A%'

This tells SQL to return all rows starting with A and having
characters after. It will NOT return rows where the entire value is A.

You can of course do: Like 'AL%'
Like 'Almos%' etc.

The % sign is critical.

Note: this does not make use of your indexes. On large tables it can
be slow, it is definitely faster that doing a where right(FieldA, 1) =
'A'.

"Dishan Fernando" <dishan (AT) gmail (DOT) com> wrote

Quote:
Hi,
I want to search Strings Like this.
Say Column containing ...
"A B"
"A B"
"A B"
"A B"
"A B"

How can I search "A B" with ignoring spaces. Result must return all the
records.

Thank you.
Dishan

Reply With Quote
  #9  
Old   
Madhivanan
 
Posts: n/a

Default Re: SQL Like. - 04-06-2005 , 07:47 AM




Dishan,
Try this also

select column from table where replace(column,' ','')='AB'


Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.