Getting last 5 char of string with mysql query - mysql

I have to get last 5 numbers using mysql.
My values are like YOT-A78514,LOP-C4521 ...
I have to get only last five char . How can I do this in query?

You can do this with RIGHT(str,len) function. Returns the rightmost len characters from the string str,
Like below:
SELECT RIGHT(columnname,5) as yourvalue FROM tablename

"Right"-function is the way to, using the substring may lead to an problem that is not so easy to notice:
mysql> select right('hello', 6);
+-------------------+
| right('hello', 6) |
+-------------------+
| hello |
+-------------------+
1 row in set (0.00 sec)
mysql> select substring('hello', -6);
+------------------------+
| substring('hello', -6) |
+------------------------+
| |
+------------------------+
1 row in set (0.00 sec)
But if you don't try to go past the start of the string, then substring of course works fine:
mysql> select substring('hello', -5);
+------------------------+
| substring('hello', -5) |
+------------------------+
| hello |
+------------------------+
1 row in set (0.00 sec)

Right is a good choice but you can also use substring like this-
SELECT Substring(columnname,-5) as value FROM table_name

SELECT row_id
FROM column_name
WHERE column_value LIKE '%12345';
This will return the "row_id" when "12345" is found to be the tailing suffix of the "column_value" within the "column_name".

And if you want to get a dinamic number of right characters after a character:
SELECT TRIM(
RIGHT(
database.table.field,
(LENGTH(database.table.field) - LOCATE('-',database.table.field))
)
)
FROM database.table;

SELECT SUBSTR('Stringname', -5) AS Extractstring;

Related

Finding a column value with only a line break in MySQL

I have a MySQL DB table where a text column has some values which seem to be only CR and LF control characters (the value is just the line break).
I need a query which will identify all such rows. I tried something like this
SELECT * FROM mytable WHERE mycolumn REGEXP "\r\n";
from here. But that didn't work. I guess I just need the correct regex in my case. Any suggestions would be greatly appreciated.
I inserted a col with line breaks, i could retrieve it with foll SQL
mysql> select lat from TEST_INSERT where lat regexp '.*[\n]';
+--------------+
| lat |
+--------------+
| xx
yy
zz
|
+--------------+
1 row in set (0.00 sec)
WHERE col = "\r\n"
will check for that column having only a Windows-type line break.
WHERE col = "\n"
for unix-style.
Is it what you are looking for???
mysql> select lat from TEST_INSERT where lat regexp '^\n+$';
+-------+
| lat |
+-------+
|
|
+-------+
1 row in set (0.00 sec)

can a mysql select query in C language return a field with the special characters escaped?

1- string = a'b"c\d
2- escaped_string = a\'b\"c\\d
3- make an insert query that inserts escaped_string in some table field.
4- make a select query that returns the inserted value.
The returned value is: a'b"c\d
Is there a way to get the select query to return a\'b\"c\\d ?
(I understand that i can escape it again).
You can use the QUOTE() function of mysql:
mysql> select data from x;
+---------+
| data |
+---------+
| a'b"c\d |
+---------+
1 row in set (0.00 sec)
mysql> select quote(data) from x;
+-------------+
| quote(data) |
+-------------+
| 'a\'b"c\\d' |
+-------------+
1 row in set (0.00 sec)
This should exactly do what you are looking for. Note that the " doesn't need to be escaped here, so QUOTE() doesn't escape it, too.

How to extract the rows that contain only 1 "=" by using REGEXP in MySQL?

Suppose I have a table named "t1" which contains a column named "ID" which has the following records
abcde=1=2
qwert=3
hhhhj=9
zxcv=5=8
How can I extract the records that contain only 1 "=" sign by using REGEXP in MySQL?
I've tried
SELECT * FROM t1 WHERE ID REGEXP '\\w*=\\w*=\\w*'; -- returns no records
SELECT * FROM t1 WHERE ID REGEXP '\\w*=\\w*'; -- returns all 4 records
I expected the first query to return the records which contains 2 "=", and the second query to return the records which contains only 1 "=".
What's wrong with my queries?
A pretty simple solution without REGEXP would be
select * from table
where
length(ID) - length(replace(ID,'=','')) = 1 ;
Some test cases
mysql> select length('qwert=3') - length(replace('qwert=3','=','')) as diff;
+------+
| diff |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> select length('zxcv=5=8') - length(replace('zxcv=5=8','=','')) as diff;
+------+
| diff |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
SELECT * FROM t1 WHERE ID REGEXP '^[^=]*=[^=]*$';
Guess this should do it.See fiddle http://www.sqlfiddle.com/#!9/b2ead/2/0

MySQL Escape Character Issue

While I was trying to solve This Question. I created the dummy records in a table
create table mytable(data CHAR(30));
INSERT INTO mytable VALUES('d\\one'),('d\\two'),('d\\three');
SELECT * FROM mytable;
+---------+
| data |
+---------+
| d\one |
| d\two |
| d\three |
+---------+
3 rows in set (0.00 sec)
Now when i am selecting records, I am getting no result, I have tried many combination with like but no luck.
Ex :
SELECT * FROM mytable WHERE data LIKE "d\\%";
Empty set (0.00 sec)
SELECT * FROM mytable WHERE data LIKE 'd\\%';
Empty set (0.00 sec)
Use triple slash:
SELECT * FROM mytable WHERE data LIKE "d\\\%"
Or use INSTR() instead
SELECT * FROM mytable WHERE instr(data, 'd\\') = 1

mysql wildcards % vs %%

What is the difference in '%' and '%%', when used in mysql where clause with 'LIKE' ?
select * from `wp_users` u where u.user_nicename like "%lastuser%"
VS
select * from `wp_users` u where u.user_nicename like "%%lastuser%%"
There is no difference between %% and % when it comes to pattern matching in mysql.
I've seen developers get confused over this when they try to match a literal % and therefor write %%. This is most often because of the fact that format-strings often use a double % to indicate that you'd like it to be treated as an exact literal.
MySQL documentation of LIKE
MySQL 5.0 Reference Manual :: 11.5.1 String Comparison Functions :: LIKE
What's the origin of the string, and where is it going?
If the string is passed to a function such as sprintf the format-string rule I mentioned earlier is present, though there is no confusion in that case.
The developer want it to be a single % in the string passed to mysql, and therefor wrote %%.
$query = sprintf (
"SELECT ... FROM ... WHERE id <> %d AND data LIKE '%%hello world%%'",
50
);
// $query => "SELECT ... FROM ... WHERE id <> 50 AND data LIKE '%hello world%'";
A few sample SELECTs using the LIKE operator
mysql> SELECT 'abc' LIKE 'ab%';
+------------------+
| 'abc' LIKE 'ab%' |
+------------------+
| 1 |
+------------------+
1 row in set (0.01 sec)
mysql> SELECT 'abc' LIKE 'ab%%';
+-------------------+
| 'abc' LIKE 'ab%%' |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT 'abc' LIKE 'ab\%';
+-------------------+
| 'abc' LIKE 'ab\%' |
+-------------------+
| 0 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT 'ab%' LIKE 'ab\%';
+-------------------+
| 'ab%' LIKE 'ab\%' |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)