SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query a Field and Ignore Commas and Periods in DB Value


Query a Field and Ignore Commas and Periods in DB Value

Author
Message
mcromarty
mcromarty
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 1
Hi,

I'm not sure how to query a field that contains commas and periods with like.

Value in db: "Acme Products, Inc."

User is querying with "Acme Products Inc"

Of course, using a like statement will not return any values. How can I alter the query to ignore any punctuation marks?

Thanks!!
SGT_squeequal
SGT_squeequal
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4835 Visits: 1208
you could try
where replace (column_name,',','')='Acme Products Inc'

or look to have the DB changed to include a csoundex column or even

where soundex (column_name) =soundex('Acme Products Inc')

*************************************************************The first is always the hardestw00t
saravanatn
saravanatn
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1340 Visits: 1127
mcromarty - Thursday, June 14, 2018 9:10 AM
Hi,

I'm not sure how to query a field that contains commas and periods with like.

Value in db: "Acme Products, Inc."

User is querying with "Acme Products Inc"

Of course, using a like statement will not return any values. How can I alter the query to ignore any punctuation marks?

Thanks!!

See the below illustration:
create table punch
(
word varchar(200)
);

insert into punch values('Acme Products, Inc.');
select * from
(select replace(replace(word,',',''),'.','') as word from punch
)rep
where word='Acme Products Inc'



Saravanan
mcromarty
mcromarty
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 1
Thanks for the quick replies. Perfect!
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)

Group: General Forum Members
Points: 352788 Visits: 41680
Just know that this will most likely result in a table or clustered index scan. Depending on the query and indexes you may get a non clustered index scan.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
sgmunson
sgmunson
SSC Guru
SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)

Group: General Forum Members
Points: 74203 Visits: 6355
mcromarty - Thursday, June 14, 2018 9:34 AM
Thanks for the quick replies. Perfect!

What Lynn Pettis posted means that performance could be a problem because every time you run the querys posted, your SQL server will have to look at every single row, and the indexes won't help much. If you have this kind of query running from a web page, you might want to consider creating a computed, persisted column that contains the data without any punctuation, and then index the table on that column, including any other columns that would get selected from that table.


Steve
‌(aka sgmunson)
Smile Smile Smile
Health & Nutrition
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)

Group: General Forum Members
Points: 352788 Visits: 41680
sgmunson - Thursday, June 14, 2018 10:45 AM
mcromarty - Thursday, June 14, 2018 9:34 AM
Thanks for the quick replies. Perfect!

What Lynn Pettis posted means that performance could be a problem because every time you run the querys posted, your SQL server will have to look at every single row, and the indexes won't help much. If you have this kind of query running from a web page, you might want to consider creating a computed, persisted column that contains the data without any punctuation, and then index the table on that column, including any other columns that would get selected from that table.

Which will work as long as the index doesn't exceed 900 bytes clustered or 1700 bytes non clustered (this is posted in a SQL 2017 forum).


Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (786K reputation)SSC Guru (786K reputation)SSC Guru (786K reputation)SSC Guru (786K reputation)SSC Guru (786K reputation)SSC Guru (786K reputation)SSC Guru (786K reputation)SSC Guru (786K reputation)

Group: General Forum Members
Points: 786048 Visits: 45914
We solved this problem by creating a persisted computed column that contains no punctuation and no spaces. When the user types something to search for, we also strip that of punctuation and spaces and then do the compare. Makes for very nice SARGable predicates.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search