M
M
MrTNTminer2020-06-28 09:21:11
PHP
MrTNTminer, 2020-06-28 09:21:11

How to make MySQL treat underscore as text?

How to make SQL treat underscores as text (and any other characters in general).
For example, I want to find data in a column strictly equal to Alexey, he calmly finds and issues, but if I enter Alex_ey, he will not find anything, because SQL replaces "_" with any character, which is very annoying.
LIKE %% doesn't match because it looks for all matches, but I need a strict one.
I use the RedBeanPHP library, code example below.

$searchlogin = R::getAll('select * from teleg where nik = :login',
                    array(':login'=>$lgtg)
                );

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2020-06-28
@MrTNTminer

Remove underlining.
Or LIKE ESCAPE

s_t_u_v_w and click find, I should display all strict matches with this nickname

SELECT * FROM teleg WHERE nik = 's_t_u_v_w'
Or
SELECT * FROM teleg WHERE nik LIKE 's!_t!_u!_v!_w' ESCAPE '!'

At the moment, due to the fact that the _ symbol is replaced, I can not find nicknames with his participation.
SQL Server does not replace anything.
put enters in the cells, because of this nothing was found

SELECT * FROM teleg WHERE nik = 's_t_u_v_w' + CHAR(13)

Or
SELECT * FROM teleg WHERE REPLACE(nik, CHAR(13), '') = 's_t_u_v_w'

Not all cells have some kind of character (or whatever it is).

SELECT CONVERT(VARCHAR, CAST(nik AS binary), 2) FROM teleg

Find your wrong symbol. For example, CHAR(13)or CHAR(0x0D)and remove it
UPDATE teleg SET nik = REPLACE(nik, CHAR(0x0D), '')

TRIMfails because space CHAR(0x20)is not the last character.
MySQL:
SELECT HEX(nik) FROM teleg
UPDATE teleg SET nik = REPLACE(nik, CAST( 0x0D AS CHAR ), '')

N
N, 2020-06-28
@Fernus

And does religion not allow the old "children's" method of shielding to be applied?

$str_like = str_replace(['_', '%'], ['\_', '\%'], 'Alex_ey');

$sql = "SELECT * FROM `table` WHERE `field` LIKE '".$str_like."'";

Well, let's not forget about sql injections... the above request is not safe if $str_like comes xs from someone... that's another topic...
PS: I read the comments to the previous answer... it seems to be the approach data storage...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question