LoginSignup
3
1

More than 1 year has passed since last update.

「’」が入った文字列があるときのSQLの書き方

Posted at

前提として

NUMBER NAME
1 Tom
2 Jennifer
3 O'neal
4 Jerry

こんなデータベースがあったとして勉強していきましょう

問題

あなたはO'nealさんの情報だけ見たいです。

普通に考えたら、

select * from 表 where name='O'neal';

となりますよね。

ですが!!

スクリーンショット_20221212_154411.png
Oracleだとこんなエラーが出て来てしまいました!

解決法

  • 二つの「'」をつづけて書く(”ではない!)
select * from 表 where name='O''neal';
  • 代替引用メカニズムを使用する
    何言ってんのですよね(笑)
    まぁ、単語は難しいですがやってることは簡単です
    初め:q'と任意の文字
    終わり:任意の文字と'を書くだけ!!
select * from 表 where name= q'|O'neal|';

とか、

select * from 表 where name= q'#O'neal#';

はたまた、

select * from 表 where name= q'{O'neal}';

こんなのとか

これでやってみた結果…

スクリーンショット_20221212_164319.png
はい!うまく取り出せました!!
これでうっとおしいあの子も一件落着!!

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1