Mysql parse dates in this format: 2012-03-27T03:03:00? - mysql

I have a table which stores times as a simple varchar, in a format that looks like "2012-03-27T03:03:00".
I'd like to use mysql's date functions with this data. Some functions work fine, i.e.
mysql> select year('2012-03-14T11:28:32'), month('2012-03-14T11:28:32');
+-----------------------------+------------------------------+
| year('2012-03-14T11:28:32') | month('2012-03-14T11:28:32') |
+-----------------------------+------------------------------+
| 2012 | 3 |
+-----------------------------+------------------------------+
1 row in set (0.00 sec)
But hour and minute functions fail:
mysql> select hour('2012-03-14T11:28:32'), minute('2012-03-14T11:28:32');
+-----------------------------+-------------------------------+
| hour('2012-03-14T11:28:32') | minute('2012-03-14T11:28:32') |
+-----------------------------+-------------------------------+
| 0 | 20 |
+-----------------------------+-------------------------------+
1 row in set, 2 warnings (0.00 sec)
I believe it is the 'T' in my date format that screws up mysql. If I manually replace it with a space, the functions work:
mysql> select hour('2012-03-14 11:28:32'), minute('2012-03-14 11:28:32');
+-----------------------------+-------------------------------+
| hour('2012-03-14 11:28:32') | minute('2012-03-14 11:28:32') |
+-----------------------------+-------------------------------+
| 11 | 28 |
+-----------------------------+-------------------------------+
1 row in set (0.00 sec)
Is there some simple way I can tell mysql to parse the 'T'???

Those MySQL Date and Time functions (HOUR, for example) require a specific part of the timestamp.
Therefore, you need to use TIME and DATE to extract the time and date, respectively, from the passed timestamp:
Extracting the time:
select hour(time('2012-03-14T11:28:32')), minute(time('2012-03-14T11:28:32'));
Extracting the date:
select year(date('2012-03-14T11:28:32')), month(date('2012-03-14T11:28:32'));

SELECT HOUR(STR_TO_DATE('2012-03-14T11:28:32','%Y-%m-%dT%k:%i:%s'))
STR_TO_DATE is pretty good about returning NULL for rubbish dates like this non-leap-year item.
SELECT STR_TO_DATE('2011-02-29T11:28:32','%Y-%m-%dT%k:%i:%s')
It also lets you do stuff like
SELECT STR_TO_DATE('2011-02-28T11:28:32','%Y-%m-%dT%k:%i:%s') + INTERVAL 1 QUARTER
or
SELECT STR_TO_DATE('2011-02-28T11:28:32','%Y-%m-%dT%k:%i:%s') - INTERVAL 35 MINUTE
and have everything work right.
See here for the conversion specifiers.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

Related

Date comparison in query: Correct logic but wrong result

I have this very simple query. But it's returning wrong result.
select * from `table_a` where date(`schedule`) > '2021-11-13 06:31:00'
In my table, I have these rows
table_a
+---------------------+
| schedule |
+---------------------+
| 2021-11-13 08:59:00 |
+---------------------+
| 2021-11-13 08:59:00 |
+---------------------+
| 2021-11-13 08:59:00 |
+---------------------+
Technically, this query should return all rows. But 0 rows is returned.
I tried changing the operation to <and it returned all rows (which is the opposite) . Did I miss something here?
note: schedule datatype is DATETIME.
Your schedule column is datetime, with a time component, so remove the cast to date:
SELECT * FROM table_a WHERE schedule > '2021-11-13 06:31:00';
What was happening is that your 3 records were all being casted to 2021-11-13 at midnight, which is the same as 2021-11-13 00:00:00. It should be clear that all of the 3 records occurred past midnight on this date.

How to correctly string token in MySQL

I realize we can use substring() and locate() function for tokonization
I was tried query
insert into sum_of_counts
select substring(pair,1,locate('|',pair)),
sum(count)
from em
group by substring(pair,1,locate('|',pair))
for example : we use 'resumption|||resumption' as pair to query after substring('resumption|||resumption',1,locate('|','resumption|||resumption'))
it should be resumption|
but after the query it appeared
+------------+------+
| wild_pair | sum |
+------------+------+
| resumption | 8 |
+------------+------+
the problem is we could find 'resumption|||resumption' in em table
after I check the table sum_of_counts some of wild_pair are word| some of wild_pair are just word how can I solve this problem?
SELECT
LEFT(`pair`, LOCATE('|||', `pair`)) `wild_pair`,
SUM(count) `sum`
FROM `em`
GROUP BY `wild_pair`;
Should do the same thing easier.
If the error occurs when inserting the result into another table, check if the existing columns are wide enough to take the calculated data in full length.
Your result should not be possible:
mysql> select locate('|', 'resumption|||resumption');
+----------------------------------------+
| locate('|', 'resumption|||resumption') |
+----------------------------------------+
| 11 |
+----------------------------------------+
1 row in set (0.00 sec)
mysql> select substring('resumption|||resumption', 1, 11);
+---------------------------------------------+
| substring('resumption|||resumption', 1, 11) |
+---------------------------------------------+
| resumption| |
+---------------------------------------------+
1 row in set (0.00 sec)
Are you sure that pair is always foo|||bar with 3 |||?

Failed mysql date query

I want to delete rows from a table that has a column more than 7200 secs old.
The Query
mysql_query("delete from logged where DATE_ADD ( log, INTERVAL $this->loginDuration SECOND) < NOW()",$this->link);
where:
name of table = logged;
name of column = log;
$this->loginDuration = 7200;
The value of log in db: 2011-06-25 09:56:51.
Todays date and time [ Now() ] : 2011-07-05 11:39:02
The query is meant to delete the row with log value 2011-06-25 09:56:51 because it is older than 7200 seconds but it does not.
What am I not getting right?
You have a space between DATE_ADD and the parenthesis: ( log, ....
Use DATE_ADD( log, ...
From MySQL docs, Functions and Operators :
Note
By default, there must be no
whitespace between a function name and
the parenthesis following it. This
helps the MySQL parser distinguish
between function calls and references
to tables or columns that happen to
have the same name as a function.
However, spaces around function
arguments are permitted.
And:
You can tell the MySQL server to
accept spaces after function names by
starting it with the
--sql-mode=IGNORE_SPACE option. (See Section 5.1.6, “Server SQL Modes”.)
Individual client programs can request
this behavior by using the
CLIENT_IGNORE_SPACE option for
mysql_real_connect(). In either case,
all function names become reserved
words.
I wanted to show you what I did (could not post this as a comment)
+----------------------+
| temp.d(table.column) |
+----------------------+
| 2011-07-05 22:08:20 |
| 2011-07-05 22:08:20 |
| 2011-07-05 22:08:21 |
| 2011-07-05 22:08:21 |
| 2011-07-05 22:08:22 |
| 2011-07-05 22:08:22 |
| 2011-07-05 22:08:23 |
| 2011-07-05 22:08:23 |
| 2011-07-05 22:08:24 |
| 2011-07-05 22:08:24 |
+----------------------+
10 rows in set (0.00 sec)
mysql> delete from temp where DATE_ADD(d, INTERVAL 1 SECOND) < NOW();
Query OK, 10 rows affected (0.01 sec)

SQL (mysql) - If a given row on a given column as a certain value, don't list that column

I have a query that retrieves some data, among those data I have some that are returned with a value like 0. I would like the query to NOT return the columns when that's the case.
How can we do such a thing?
Regards,
MEM
select <column_name> from <table_name> where <column_name> <> 0.0
Here is all the data in a sample database. Notice how there are 3 rows with one having a zero value for the num column.
mysql> select * from test_tbl;
+------+----------+
| num | some_str |
+------+----------+
| 0 | matt |
| 2 | todd |
| 3 | Paul |
+------+----------+
3 rows in set (0.00 sec)
Now lets use the where clause to specify the rows we want to ignore (it's a little bit of reverse logic because we are actually specifying what rows we want).
mysql> select * from test_tbl where num <> 0.0;
+------+----------+
| num | some_str |
+------+----------+
| 2 | todd |
| 3 | Paul |
+------+----------+
2 rows in set (0.00 sec)
Note: This will only work without getting messy if 0 is the only value you are worried about. A better way would be to allow nulls in your column and then you can check to see if they are non-null in the where clause.

Why does the minus operator give different result than the TIMESTAMPDIFF() function in mysql?

Since TIMESTAMP in mysql is stored as a 32bit value representing the time interval from 1970-jan-1 0:00:00 in seconds, I assumed that using minus (-) operator on TIMESTAMP values would give the difference of these values in seconds. Actually not:
+---------------------------------------------------------------------+
| TIMESTAMP("2010-04-02 10:30:00") - TIMESTAMP("2010-04-02 10:29:59") |
+---------------------------------------------------------------------+
| 41.000000 |
+---------------------------------------------------------------------+
1 row in set (0.05 sec)
mysql> select timestampdiff(SECOND,TIMESTAMP("2010-04-02 10:30:00"),TIMESTAMP("2010-04-02 10:29:59"));
+-----------------------------------------------------------------------------------------+
| timestampdiff(SECOND,TIMESTAMP("2010-04-02 10:30:00"),TIMESTAMP("2010-04-02 10:29:59")) |
+-----------------------------------------------------------------------------------------+
| -1 |
+-----------------------------------------------------------------------------------------+
mysql> select TIMESTAMP("2010-04-02 10:30:00") - TIMESTAMP("2010-04-02 10:30:01") ;
+---------------------------------------------------------------------+
| TIMESTAMP("2010-04-02 10:30:00") - TIMESTAMP("2010-04-02 10:30:01") |
+---------------------------------------------------------------------+
| -1.000000 |
+---------------------------------------------------------------------+
+---------------------------------------------------------------------+
| TIMESTAMP("2010-04-02 10:30:00") - TIMESTAMP("2010-04-02 10:31:00") |
+---------------------------------------------------------------------+
| -100.000000 |
+---------------------------------------------------------------------+
It seems like one minute difference is 100 instead of 60.
Why is this?
Just a wild guess, but maybe you're casting the strings to an integer in three of the cases?
20100402103000 - 20100402103100 = -100
20100402103000 - 20100402103001 = -1
20100402103000 - 20100402102959 = 41
The other case does the conversion properly.
The correct function to use is UNIX_TIMESTAMP().
TIMESTAMP() returns a date(time), in the format '2003-12-31 00:00:00'.
Also, there is nothing wrong with using
SELECT TIMESTAMPDIFF(SECOND,NOW(),TIMESTAMP("2010-04-02 19:29:59"));
I tried the following and received NULL
SELECT TIMESTAMPDIFF(SECOND, TIMESTAMP("stopTime"), TIMESTAMP("startTime")) FROM
paktime WHERE fs = 1 AND employ LIKE "Mike Fowler"