I have a table of questions. I need to find rows which have '?' in the question text because of bad character encoding/collation. I need to find all the rows which have '?' but also need to ignore the question marks at the end of the questions. I tried this query but I still get rows with questions marks at end of the question
SELECT *
FROM `kc_questions`
WHERE `question` LIKE "%?%" /* WHICH CONTAINS '?' */
AND `question` NOT LIKE "%?" /* DOES NOT END WITH '?' */
EDIT: phpmyadmin actually tells me there is something wrong with the query:
The query however runs successfully returning rows which end with'?'.
Based on the sample data I tried the following demo and it works as expected.
SQL:
create table kc_questions(question varchar(200));
insert into kc_questions values
('Ex1. ?-particles are harmul for human body. Select True or False.'),
('Ex2. What is your name?');
SELECT question FROM kc_questions;
SELECT *
FROM `kc_questions`
WHERE `question` LIKE "%?%"
AND `question` NOT LIKE "%?";
Output:
mysql> SELECT question FROM kc_questions;
+-------------------------------------------------------------------+
| question |
+-------------------------------------------------------------------+
| Ex1. ?-particles are harmul for human body. Select True or False. |
| Ex2. What is your name? |
+-------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> SELECT *
-> FROM `kc_questions`
-> WHERE `question` LIKE "%?%"
-> AND `question` NOT LIKE "%?";
+-------------------------------------------------------------------+
| question |
+-------------------------------------------------------------------+
| Ex1. ?-particles are harmul for human body. Select True or False. |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.8-rc |
+-----------+
1 row in set (0.00 sec)
You could use a regular expression
SELECT *
FROM `kc_questions`
WHERE `question` REGEXP '.*\?.+$'
Basicly you search for questions which contains '?' with at least one character after the '?'
Related
I am taking data from a csv file and throwing it all into a tempory table, so everything is in string format.
So even date fields are in string format, so I need to convert date from string to a date. All dates are in this format 28/02/2013
I used STR_TO_DATE for this, but I am having a problem.
Here is a snippet of my code.
INSERT INTO `invoice` (`DueDate`)
SELECT
STR_TO_DATE('','%d/%m/%Y')
FROM `upload_invoice`
There are of course more fields than this, but I am concentrating on the field that doesn't work.
Using this command if a date is invalid it should put in a null, but instead of a null being put in, it generates an error instead.
#1411 - Incorrect datetime value: '' for function str_to_date
I understand what the error means. It means it is getting an empty field instead of a properly formatted date, but after reading the documentation it should not be throwing an error, but it should inserting a null.
However if I use the SELECT statement without the INSERT it works.
I could do the following line which actually works to a point
IF(`DueDate`!='',STR_TO_DATE(`DueDate`,'%d/%m/%Y'),null) as `DueDate`
So STR_TO_DATE doesn't run if the field is empty. Now this works, but it can't check for a date which is not valid eg if a date was ASDFADFAS.
So then I tried
IF(TO_DAY(STR_TO_DATE(`DueDate`,'%d/%m/%Y') IS NOT NULL),STR_TO_DATE(`DueDate`,'%d/%m/%Y'),null) as `DueDate`
But this still gives the #1411 error on the if statement.
So my question is why isn't STR_TO_DATE not returning NULL on an incorrect date? I should not be getting the #1411 error.
This is not an exact duplicate of the other question. Also there was not a satisfactory answer. I solved this a while and I have added my solution, which is actually a better solution that was given in the other post, so I think because of my better answer this should stay.
An option you can try:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.19 |
+-----------+
1 row in set (0.00 sec)
mysql> DROP TABLE IF EXISTS `upload_invoice`, `invoice`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `invoice` (
-> `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> `DueDate` DATE
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `upload_invoice` (
-> `DueDate` VARCHAR(10)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `upload_invoice`
-> (`DueDate`)
-> VALUES
-> ('ASDFADFAS'), (NULL), (''),
-> ('28/02/2001'), ('30/02/2001');
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> INSERT INTO `invoice`
-> SELECT
-> NULL,
-> IF(`DueDate` REGEXP '[[:digit:]]{2}/[[:digit:]]{2}/[[:digit:]]{4}' AND
-> UNIX_TIMESTAMP(
-> STR_TO_DATE(`DueDate`, '%d/%m/%Y')
-> ) > 0,
-> STR_TO_DATE(`DueDate`, '%d/%m/%Y'),
-> NULL)
-> FROM `upload_invoice`;
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> SELECT `id`, `DueDate`
-> FROM `invoice`;
+----+------------+
| id | DueDate |
+----+------------+
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | 2001-02-28 |
| 5 | NULL |
+----+------------+
5 rows in set (0.00 sec)
See db-fiddle.
I forgot I posted this question, but I solved this problem a while ago like this
IF(`{$date}`!='',STR_TO_DATE(`{$date}`,'%d/%m/%Y'),null) as `{$date}`
So because the line is long and confusing I made a function like this
protected function strDate($date){
return "IF(`{$date}`!='',STR_TO_DATE(`{$date}`,'%d/%m/%Y'),null) as `{$date}`";
}
INSERT INTO `invoice` (`DueDate`)
SELECT
{$this->strDate('DueDate')}
FROM `upload_invoice`
I really forgot I posted this question. It seems like an eternity away, but this is how I solved the issue.
mysql> SELECT title FROM pages WHERE id=111;
+------------+
| title |
+------------+
| 'Theology' |
+------------+
1 row in set (0.00 sec)
mysql> SELECT id FROM pages WHERE title='Theology';
Empty set (0.00 sec)
The results conflicted. I can't understand that.
Change
'Theology'
to
'\'Theology\''
Seems that the data stored is 'Theology' instead of Theology. Thanks to Abhik Chakraborty.
Use query like this . You need to escape the '
1st Way
SELECT id FROM pages WHERE title = '\'Theology\''
2nd Way
SELECT id FROM pages WHERE title = "'Theology'"
3rd Way
SELECT id FROM pages WHERE title='''Theology''';
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
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;
I have a list of strings, and I'd like to see only the ones that are not in the database.
For example I have this string: "Conducteur(trice) de bus" that is in the database:
So if I do something like:
mysql> select * from job where description = "Conducteur(trice) de bus";
+-------+--------------------------+
| id | description |
+-------+--------------------------+
| 14495 | Conducteur(trice) de bus |
+-------+--------------------------+
1 row in set (0.00 sec)
mysql>
Now if the job "Cinéaste" doesn't exist:
mysql> select * from job where description = "Cinéaste";
Empty set (0.00 sec)
mysql>
But I want the exact opposite, i.e. if the string is here I don't want a result to show up, and if it's not here, I'd like the string to show up.
Here's what I'd like for the same strings explained before:
mysql> select * from job [clause i don't know] "Conducteur(trice) de bus";
Empty set (0.00 sec)
mysql>
mysql> select * from job [clause i don't know] "Cinéaste";
+-------------+
| description |
+-------------+
| Cinéaste |
+-------------+
mysql>
So when the record is found, nothing is shown, and when it's not found, the string I was looking for is shown.
Any idea how I could do such queries with MySQL?
Edit: Try
SELECT 'Cinéaste' AS 'description' FROM `job` WHERE NOT EXISTS
(SELECT * FROM `job` WHERE description = 'Cinéaste')
LIMIT 1
You should create a table (can be a temporary table) with the list of strings to check, one per row. Let's consider you create a table named check, with a column description containing the values to be checked. Then you could use this query:
SELECT description
FROM check
WHERE description NOT IN (
SELECT DISTINCT description FROM metier
);