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)
Related
I Am trying to do this:
update student
set student_name=SUBSTRING(student_name, 0, 8)
where student_name like 'MAX%';
So, my intent is to update the column with first 8 chars of the original content.
But the student_name column is getting set to empty value.
Why is this happening? can someone help me fix this
Before update anything, do select with similar request.
MariaDB [(none)]> select SUBSTRING('123456',1,2 ), SUBSTRING('123456',0,2 );
+--------------------------+--------------------------+
| SUBSTRING('123456',1,2 ) | SUBSTRING('123456',0,2 ) |
+--------------------------+--------------------------+
| 12 | |
+--------------------------+--------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>
Invalid or negative first number in substring resulting empty string.
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.
Is this even possible?
All I want to do is search for a common string in a Wordpress database column and delete the string AND everything to the left of the string.
I was trying variations of the following:
UPDATE wp_posts
SET post_content = REPLACE(post_content, '%(1280 x 853)</p>', '')
I've since realized that you can't use REPLACE with wildcards. So is there any other way to do it?
Note: The content to the left of the common string is of varying length, so I wouldn't be able to use any code that needs to specify a certain number of characters.
And yes, I actually want to update every applicable row in the database column, not simply return a SELECT statement.
Any help would be much appreciated. My brain aged 10 years over this problem.
Given this
MariaDB [sandbox]> SELECT * FROM T;
+---------------------------+
| STR |
+---------------------------+
| AB(1280 x 853)</p>CD |
| 1234(1280 x 853)</p>56789 |
| ZYX |
+---------------------------+
this
UPDATE T
SET STR = SUBSTRING(STR,INSTR(STR,'(1280 x 853)</p>') + LENGTH('(1280 x 853)</p>'),LENGTH(STR))
WHERE INSTR(STR,'(1280 x 853)</p>') > 0
;
results in
SELECT * FROM T;
+-------+
| STR |
+-------+
| CD |
| 56789 |
| ZYX |
+-------+
Hope I understand your question clearly, you can try this:
UPDATE wp_posts
SET post_content = SUBSTRING_INDEX(post_content, '(1280 x 853)</p>', -1);
Try this,
UPDATE wp_posts SET post_content = SUBSTR(post_content, 1, (LENGTH(post_content) - 16));
If any further improved query, I will post in the comment.
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;
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)