February 17, 2020 at 8:35 pm
I want to do some match on a dataset that contains location value and the location value is to be determined its country, some data is below:
Rumney, NH, US
Hickory Hills, IL
Patchogue, NY
Wilton, NH
Cedarville University, OH
Shopshire, UK
Dreilinden, Germany
Jarjis, Tunisia
Casca/RS, Brasil
create table location
(
city varchar(50),
country varchar(10)
)
insert into location (city) values ('Rumney, NH, US')
insert into location (city) values ('Hickory Hills, IL')
insert into location (city) values ('Patchogue, NY')
insert into location (city) values ('Wilton, NH')
insert into location (city) values ('Cedarville University, OH')
insert into location (city) values ('Shopshire, UK')
insert into location (city) values ('Dreilinden, Germany')
insert into location (city) values ('Jarjis, Tunisia')
insert into location (city) values ('Casca/RS, Brasil')
I believe Regex is the best way to detect that, '(?!.*UK),\s(\w{2})$' is what I come up:
Below is my testing:
declare @pattern varchar(20)
set @pattern = '(?!.*UK),\s(\w{2})$'
select @pattern
select * from location where city like '%' + @pattern + '%'
I tested it in SQL 2008 Express, it returns nothing:
What am I missing here?
Thank you very much.
February 17, 2020 at 8:55 pm
February 17, 2020 at 9:17 pm
Thank you Lynn,
Per your link it says:
match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]
In my case I used: where city like '%' + @pattern + '%'
set @pattern = '(?!.*UK),\s(\w{2})$'
I also tried not using '%" beside the pattern, I also tried PATINDEX('%' + @pattern + '%', city)
If the above are NOT supported, how can I then use Regex to match?
Can you please enlighten?
Thank you very much.
February 17, 2020 at 9:49 pm
The answer is simple: you are not in Kansas anymore.
LIKE does not support regular expressions of any flavour. It supports % to mean 0 or more of any characters. It supports _ to mean exactly one of any character. And it supports [] to say exactly one of a set of characters, for instance [abc] or [0-9]. Beware that [a-z] will include A-Z as well, unless you have a binary collation. You can also negate the range: [^0-9].
And that is exactly what you can do, which Lynn has tried to tell you in three posts.
If you want to use real regular expression, you will need to write a CLR function that uses the RegEx classes.
[font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]
February 17, 2020 at 9:52 pm
Thank you Erland, I see only one post from Lynn, but I did receive three notification emails about her posts.
February 17, 2020 at 9:54 pm
Well, you started three threads, and Lynn replied to them all...
[font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]
February 17, 2020 at 9:57 pm
Thank you Erland, I see only one post from Lynn, but I did receive three notification emails about her posts.
A common mistake... "Lynn" is a crusty ol' dude that's almost as ugly as I am. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
February 17, 2020 at 10:08 pm
hmmm, I didn't realize I started three threads, indeed I encountered some hiccup when I posted it - the browser just hangs there - I had to re-submitted it, maybe that's why I "started three threads", my apologies.
February 17, 2020 at 10:10 pm
Here is the screenshot of my email inbox. I was wondering why Lynn would have replied three times. Sorry Lynn.
February 17, 2020 at 10:21 pm
I was trying to keep people from posting on the other threads as well, which is why two of them referenced back to the thread I actually replied on.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply