PDA

View Full Version : sprintf & '%'



matmatmat
November 1st, 2009, 11:53 AM
How do i represent the '%' character? I need to create a SQL query:


SELECT * FROM people where name LIKE '%xxx%'

The 'xxx' bit will be formatted ('%s'), how would I do this?

albandy
November 1st, 2009, 11:54 AM
\%

miklcct
November 1st, 2009, 11:55 AM
\%

You are wrong. It should be "%%"

albandy
November 1st, 2009, 12:16 PM
You are wrong. It should be "%%"

http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html

slakkie
November 1st, 2009, 12:19 PM
http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html

Still wrong. \% is to represent the % as a literal character in MySQL. '\%%' whould match on anything starting with %

With printf, to get the % character, you should use %%.

So making \% is mysql with printf would be:

printf "\%%"

albandy
November 1st, 2009, 12:27 PM
Still wrong. \% is to represent the % as a literal character in MySQL. '\%%' whould match on anything starting with %

With printf, to get the % character, you should use %%.

So making \% is mysql with printf would be:

printf "\%%"

He's asking how to represent the % character. So I answered it, I'm not writing the query.

For example if he/she has entries in a table field like "Our percentages are 20% of some thing" the query should be
select * from table where field like '%20\%%';

Edit:
sorry I didn't realize that he was asking how to write it throught sprintf, I thought he was asking how to introduce the % character in a mysql query, of course, if he want to use the % character in the query he should scape char for sprintf and the scape char for mysql query, for example:

select * from table where fielad like '\%20\\%\%'

slakkie
November 1st, 2009, 12:49 PM
it should be (in bash):



$ printf "select * from table where field = '%%%s%%'" value
select * from table where field = '%value%'

albandy
November 1st, 2009, 01:58 PM
it should be (in bash):



$ printf "select * from table where field = '%%%s%%'" value
select * from table where field = '%value%'


VAR=HELLO
printf "select * from table where field ='\045$VAR\045'"
result:
select * from table where field ='%HELLO%'