14

I have the following table:

CREATE TABLE lawyer (
  id SERIAL PRIMARY KEY,
  name VARCHAR NOT NULL UNIQUE,
  name_url VARCHAR check(translate(name_url, 'abcdefghijklmnopqrstuvwxyz-', '') = '') NOT NULL UNIQUE
);

I want to SELECT * FROM lawyer where name_url = "john-doe"

Anders Marzi Tornblad
  • 18,046
  • 9
  • 55
  • 65
Jeka
  • 1,510
  • 3
  • 19
  • 36
  • 1
    You could use `SELECT * FROM lawyer where name_url = 'john-doe'` Which is pretty much what you wrote, but with single quotes around your string literal. – JNevill Jun 16 '16 at 20:44
  • So what is your question? Don't use double-quotes for string literals, use single-quotes as in `'john-doe'`. Also, using `varchar` without specifying column width is kind of bad practice. – jpw Jun 16 '16 at 20:44

2 Answers2

32

Character literals are put into single quotes:

SELECT * 
FROM lawyer 
where name_url = 'john-doe';

See the manual for details:
https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
0

Looking for exact match:

select column1, column2 from mytable where column2 like 'string';

Pattern Match can be achieved using:(returns stringxx, stringyyy..)

select column1, column2 from mytable where column2 like 'string%';

This will return any column2 that matches string***.

For using OR condition, to compare multiple strings below one can be used:

select column1, column2 from mytable where column2 ~* 'string1|string2';
Anto
  • 2,192
  • 1
  • 14
  • 13