Use the ~
operator.
Table 9-12. Regular Expression Match Operators
Operator Description Example ~
Matches regular expression, case sensitive 'thomas' ~ '.*thomas.*'
~*
Matches regular expression, case insensitive 'thomas' ~* '.*Thomas.*'
!~
Does not match regular expression, case sensitive 'thomas' !~ '.*Thomas.*'
!~*
Does not match regular expression, case insensitive 'thomas' !~* '.*vadim.*'
For when your stringish column might contain the empty string as well as nulls.
string |
string = '' |
string = '' is false |
string = '' is not false |
---|---|---|---|
Original string | String is empty | We are sure the string is not empty (empty is false) | 'String is empty' is something other than 'surely false' |
'a' |
false |
true |
false |
'' |
true |
false |
true |
null |
null |
false |
true |
String is present: string = '' is false
String is blank: string = '' is not false
string_to_array(anyarray, delimiter [, nullstring])
Ruby array.compact!
PG array_remove(array, null)
array_length(array, 1)
Ruby array.join(delimiter)
PgSQL array_to_string(array, delimiter)
json->'key'
(returns JSON)
json->>'key'
(returns text)
The
->
operator returns ajson
result. Casting it totext
leaves it in a json reprsentation.The
->>
operator returns atext
result. Use that instead.test=> SELECT '{"car": "going"}'::jsonb -> 'car'; ?column? ---------- "going" (1 row) test=> SELECT '{"car": "going"}'::jsonb ->> 'car'; ?column? ---------- going (1 row)Answer from Craig Ringer on Stackoverflow: "Remove double quotes from the return of a function in PostgreSQL"
CREATE INDEX tbl_col_idx ON table_name (column_name);