September 20, 2004 at 3:23 pm
I hope I'm posting this in the right place......
I am trying to find out how many accounts have a name that contain an apostrophe.
So I try:
select * from table1 where name like '%'%'
But of course this is not correct, since it sees it as an unclosed quotation mark. Any ideas on how to find accounts that include an ' anywhere in the name?
Thanks for any thoughts!
September 20, 2004 at 4:07 pm
Escape it by doubling them up.
select * from table1 where name like '%''%'
September 20, 2004 at 6:53 pm
SET QUOTED_IDENTIFIER off
select * from table1 where name like "%'%"
This should work fine.
September 21, 2004 at 12:11 am
select * from table1 where col1 like '%''%'
this will do it
<a href="http://www.websolsoftware.com"> For IT jobs click here</a>
*Sukhoi*[font="Arial Narrow"][/font]
September 21, 2004 at 3:31 am
Yet another method (without having to keep track of escaped quotes)
select foo from bar where charindex(char(39), foo) > 0
/Kenneth
September 21, 2004 at 6:05 am
Or to take Kenneth's use of char(39), how about:
SELECT Bob FROM Dog WHERE Bob LIKE '%' + CHAR(39) + '%'
Pretty much the same thing really - useful when you need to build a string with quotes in it.
September 21, 2004 at 7:19 am
Thanks everyone for the replies! I will try them out..
September 21, 2004 at 7:22 am
try it
select * from table1 where name like '%''%'
September 21, 2004 at 8:58 am
I tried this and it works. you need two single quotes in the middle, instead of one double quote (didn't seem to work for me with one double quote.) I think that's what others said, but this font made it look like a double quote.
select * from table1
where [name] like '%' '%'
January 7, 2005 at 3:01 pm
I now have another question:
How do you insert data into a table that contains an apostrphe? I'm using sql 2000. I know that in sql7 I could use double quotes, but it is not working in sql 2000.
Thanks for any suggestions
January 7, 2005 at 3:12 pm
Found the answer to my own question. Should have searched longer.....
insert into table1 values ('abc''s','x')
record will read:
column1 abc's
column2 x
January 7, 2005 at 3:17 pm
If you are doing this from an application, I would recommend masking it. In other words, your application will display an apostrophe [ CHAR(39) ], but you can use the REPLACE function to change that character to ` [ CHAR(96) ] for the actual data within your database. Then you will not have any problems with switching back and forth. And, even if someone forgets to switch, that character will still read okay, albiet read a bit odd...
I wasn't born stupid - I had to study.
January 7, 2005 at 3:27 pm
create table ##mytable(name varchar(25))
insert ##mytable
select 'Manisha'''
insert ##mytable
select 'Man''isha'
select * from ##mytable
Thanks,
Ganesh
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply