views:

52

answers:

3

I have this query:

select * from table where column like '%firstword[something]secondword[something]thirdword%'

What do I replace [something] with to match an unknown number of spaces?

Edited to add: % will not work as it matches any character, not just spaces.

+1  A: 

The following may help: http://blogs.msdn.com/b/sqlclr/archive/2005/06/29/regex.aspx as it describes using regular expressions in SQL queries in SQL Server 2005

John Pickup
I found that (or a similar) article earlier. Every article I found only helped me with a known number of spaces, I am looking for unknown. Maybe it is impossible in T-SQL. Wouldn't be the first thing.
Sean
@Sean - Regular expression syntax would definitely allow this. +1. But you need to enable CLR and install an assembly that shells the .NET framework Regex object.
Martin Smith
CLR is not on option.
Sean
+4  A: 

Perhaps somewhat optimistically assuming "unknown number" includes zero.

select * 
from table where 
REPLACE(column_name,' ','') like '%firstwordsecondwordthirdword%'
Martin Smith
I think this is what I need. I have no problem with unknown including zero.
Sean
This seems to work as needed. Thanks! I made this the accepted answer.
Sean
A: 

I would definitely suggest cleaning the input data instead, but this example may work when you call it as a function from the SELECT statement. Note that this will potentially be very expensive.

http://www.bigresource.com/MS_SQL-Replacing-multiple-spaces-with-a-single-space-9llmmF81.html

Tim