开发者

SQL Server pattern matching..regular expression?

I am trying to search for P.O Box in an address field.

Is this the best solution ?

SELECT * 
FROM [Address] WITH (NOLOCK)
WHERE 
Addressline1   LIKE ('%p.o%box%')   OR 
Addressline1   LIKE ('%p.o.box%')   OR 
Addressl开发者_如何学Goine1   LIKE ('%po%box%')    OR 
Addressline1   LIKE ('%p o%box%')

Should I use regular expression ?

Thanks Jothish


The LIKE keyword has just a bit of RegEx-like capability; see http://msdn.microsoft.com/en-us/library/ms179859.aspx (just click 'Other Versions' to get corresponding page for SQL Server 2005).

You can use the underscore '_' as a "single-character wildcard", e.g. "LIKE '%P_O_Box%'" means "the letter 'P' followed by any 1 character followed by 'O' followed by any 1 character followed by 'Box'".

You can use square-brackets '[]' to look for a range or set of characters, e.g. '%P[. ]O[. ]Box%', meaning "'P' followed by a space or a period (and nothing else), followed by 'O', followed by a space or period, followed by 'Box'".

Side-note: You don't need the parenthesis around the comparison-strings.

But ultimately, as billinkc suggested, you should standardize the data in this field if at all possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜