Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql : Search field contain tab character. (With LIKE)

Tags:

postgresql

I'm searching how to check if any field contains the TAB character.

after read this post, I tried to use this command :

SELECT id FROM account WHERE description LIKE '%\t%';

But it returns me all fields that contain the 't' character.

Do you have any solution to represent the TAB character ?

like image 212
Samuel Dauzon Avatar asked Dec 23 '14 08:12

Samuel Dauzon


People also ask

How do I find the field containing the tab character in SQL Server?

select * from MyTable where Field1 '%' || CHR(9) || '%'; Other solution can be: Select * from MyTable where instr(Field1, CHR(9)) <> 0; Cheers!

How do I find special characters in PostgreSQL?

SELECT * FROM spatial_ref_sys WHERE srtext LIKE '%\ /%'; Sometimes these ticks are very useful for searching special characters in a database.

How do I use like keyword in PostgreSQL?

The PostgreSQL LIKE operator is used to match text values against a pattern using wildcards. If the search expression can be matched to the pattern expression, the LIKE operator will return true, which is 1. The percent sign represents zero, one, or multiple numbers or characters.

How do I remove special characters from a string in PostgreSQL?

PostgreSQL LTRIM, RTRIM, and BTRIM functionsThe LTRIM() function removes all characters, spaces by default, from the beginning of a string. The RTRIM() function removes all characters, spaces by default, from the end of a string. The BTRIM function is the combination of the LTRIM() and RTRIM() functions.


3 Answers

you have to use a literal tab chacater,

 SELECT id FROM account WHERE description LIKE '%   %';

In psql type ctrl-V and then TAB to enter a tab. in other environments there are other ways to enter literal tabs.

Alternatively you can use escape string syntax: e'%\t%', or octal escape e'%\011%'.

like image 93
Jasen Avatar answered Oct 17 '22 14:10

Jasen


In SQL there is no "escaping" of characters like \t. You can use the chr() function for this:

select id
from account
where description LIKE '%'||chr(9)||'%'

I prefer the strpos function in this case, because I think it makes the intention clearer

select id
from account
where strpos(description, chr(9)) > 0
like image 25
a_horse_with_no_name Avatar answered Oct 17 '22 15:10

a_horse_with_no_name


I'm very late to this party, but having looked into this today, I've found you can also use regular expression.

SELECT id FROM account WHERE description ~ '\t';
like image 3
Andy Norris Avatar answered Oct 17 '22 14:10

Andy Norris