Showing posts with label mysql_select. Show all posts
Showing posts with label mysql_select. Show all posts

Thursday, December 6, 2012

How do I escape single quotes in SQL queries?

INVALID:

SELECT * FROM TableName WHERE FieldName = 'QueryString's Value'

VALID:

SELECT * FROM TableName WHERE FieldName = 'QueryString''s Value'

Tuesday, November 27, 2012

How to find the length of a chinese phrase in a MySQL database with SQL

create table word(
  en_word text null,
  zh_word text null
);

insert into word values('Internet', '互联网');
insert into word values('Hello', '你好');

INVALID QUERY:
select
  LENGTH(en_word) as 'English Length',
  LENGTH(zh_word) 'Zh word length'
from word;

VALID QUERY:
select
  CHAR_LENGTH(en_word) as 'English Length',
  CHAR_LENGTH(zh_word) 'Zh word length'
from word;


















LENGTH returns length in bytes (and chinese is multibyte)
Use CHAR_LENGTH to get length in characters

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_char-length
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_length