I'm trying to select a date field which is not in a standard format and only select dates that are older than X number of days. Done some searching and found some examples here but I can't seem to get mine to work.
My date format is like this: Wed Dec 3 09:00:46 2014
which is located in a column: rundate (TEXT)
SELECT * FROM myTable WHERE rundate < DATEADD((day, -5, GETDATE()), '%a %b %e %T %Y')
I get error like: FUNCTION myDB.DATEADD does not exist
Any idea how I can get this to work? My end goal is to delete old records but for now selecting them would be great! Hope you can help.
Cheers
The function you're looking for is date_add (note the underscore).
Moreover, instead of comparing text representations of dates (i.e., converting both to strings), you should be converting the string to a date:
SELECT *
FROM myTable
WHERE STR_TO_DATE(rundate, '%a %b %e %T %Y') <
DATE_ADD(CURDATE(), INTERVAL -5 DAY)
Also, if you want to subtract dates, then date_sub may be a tad more intuitive:
SELECT *
FROM myTable
WHERE STR_TO_DATE(rundate, '%a %b %e %T %Y') <
DATE_SUB(CURDATE(), INTERVAL 5 DAY)
Related
I have a column (lastlogin) that contains the following value = '17th May 2017 09:40:43 AM' ----
php function to create stamp:
date('jS F Y h:i:s A');
How can I do a select query that shows the (lastlogin) in that last 30 minutes from current time?
I'm guessing we have to convert date/time to string and separate the 2?
Any help would be amazing.
UPDATE:
I've tried the following but did not work returned more aless of records in table:
SELECT user_id, ('lastlogin' >= DATE_FORMAT(NOW() - INTERVAL 30 MINUTE,'%D %M %Y %H:%m:%s %p')) as result
FROM login_last
where lastlogin >= DATE_FORMAT(NOW() - INTERVAL 30 MINUTE,'%D %M %Y %H:%m:%s %p')
GROUP BY user_id
TABLE Structure
[id] int(8)
[user_id] int(11)
[lastlogin] varchar(30)
[browser] varchar(300)
You will need to convert your string date to a datetime format for the comparison, then compare the current datetime to the value stored.
You could also convert them to sortable strings for comparison. Something like "2017-05-17 10:20:10 AM", but it's easier to compare using the datetime type.
Reference:
str_to_date
date_format
MySQL Query:
SELECT
`user_id`,
`last_login`,
(STR_TO_DATE(`lastlogin`,'%D %b %Y %h:%i:%s %p') >= (NOW() - (INTERVAL 30 MINUTE))) as `in_range`
FROM `login_last`
WHERE (STR_TO_DATE(`lastlogin`,'%D %b %Y %h:%i:%s %p') >= (NOW() - (INTERVAL 30 MINUTE)))
SELECT * FROM tablename WHERE lastlogin >= DATE_SUB(NOW(), INTERVAL 30 MINUTE);
The challenge is to convert the result of DATE_SUB(NOW(), INTERVAL 30 MINUTE) to the correct format '17th May 2017 09:40:43 AM' (or format the left condition alias your lastLogin to UNIX_TIMESTAMP / datetime). In case you are inserting the date values yourself you probably already know how to get that format and can complete that yourself. Otherwise I recommend you to create a new question which is about formatting UNIX_TIMESTAMPs to your desired format.
A blind guess without having the chance to try that on my on would be:
SELECT * FROM tablename
WHERE UNIX_TIMESTAMP(lastlogin) >= DATE_SUB(NOW(), INTERVAL 30 MINUTE);
I have a table which contains a varchar field containing date like '15 May 2015 - 03:10 am'
I have to compare all date in this table with the current date to retrieve row which are next the current date and row which are recent to the current date
I found this solution here:
select *
from reunion a
where STR_TO_DATE(a.dateDebut,'%d %b %Y - %I:%i %p')>
DATE_FORMAT(NOW(),'%d %b %Y - %I:%i %p');
but it returns all date: '15 May 2013 - 03:10 AM' ,'15 May 2015 - 03:10 am','15 May 2016 - 03:10 am'
and when I change to "<" it returns 0 rows;
is there any other solution to give the varchar field the ability to be a valid date?
Your query is overkill. You just need to convert your date format to a date. now() is already a date. So try:
select *
from reunion a
where STR_TO_DATE(a.dateDebut,'%d %b %Y - %I:%i %p') > NOW();
select COUNT(DISTINCT devices) AS "Devices" from measure_tab where
measure_tab.time >= 1375243200 and
measure_tab.time < 1375315200;
The output of the above sql query gives a table with a column named "Devices" with number of devices. I want the time which is one of the attributes of measure_tab should also get displayed in another column of the output table with the UNIX TIME which is 1375243200 in this query converted into the SQL datetime format which is Thu Aug 1 00:00:00 UTC 2013.
Can someone help me out?
Thanks
You can use function as below:
select FROM_UNIXTIME(UNIX_TIMESTAMP(),'%a %b %d %H:%i:%s UTC %Y');
output will be:
'Wed Feb 05 05:36:16 UTC 2014'
In your query
select COUNT(DISTINCT devices) AS "Devices",
FROM_UNIXTIME(measure_tab.time,'%a %b %d %H:%i:%s UTC %Y') as d from measure_tab where
measure_tab.time >= 1375243200 and
measure_tab.time < 1375315200;
For more info you can check documentation: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
You can see sql fiddle:http://sqlfiddle.com/#!2/a2581/20357
In Mysql you can use from_unixtime() function to convert unix timestamp to Date:
select COUNT(DISTINCT devices) AS "Devices" from measure_tab where
measure_tab.time >= from_unixtime(1375243200) and
measure_tab.time < from_unixtime(1375315200);
you could use FROM_UNIXTIME inside DATE_FORMAT, but luckily,
FROM_UNIXTIME also accepts a format string, so you could just use it
by itself
Like this
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x')
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
As detailed in the other answers, FROM_UNIXTIME is the function you are looking for. Please be aware that this implicitly takes into account the local time zone setting on the machine running MySQL.
There's lots of useful information here:
Should MySQL have its timezone set to UTC?
if you need to find out how to check/set the time zone.
Problem
I am trying to fetch all the records from table where date_time field is greater than 'Thu, 11 Jul 2013' by running the below mentioned query. Value in the date_time field is stored in this format => Thu, 11 Jul 2013 08:29:37. Any help will be great.
Datatype of field date_time is varchar
Query
SELECT * FROM table_name
WHERE username = 'mark#example.com'
AND STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= 'Thu, 11 Jul 2013 00:00:00';
Here is yet another great example of why you should implement date/time fields in MySQL using the date, datetime, or timestamp field types and let your application deal with how to format the date for output.
Right now you are going to need to do something like:
SELECT * FROM table_name
WHERE username = 'mark#example.com'
AND STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= STR_TO_DATE('Thu, 11 Jul 2013 00:00:00', '%a, %e %b %Y %H:%i:%s');
This query will not be able to use any index you have on your date_time field, so the query will be very inefficient. It will need to perform a full table scan, converting the value of each row in order to make the comparison.
What you should be doing is:
SELECT * FROM table_name
WHERE username = 'mark#example.com'
AND date_time >= STR_TO_DATE('Thu, 11 Jul 2013 00:00:00', '%a, %e %b %Y %H:%i:%s');
Here if you have your field in the MySQL datetime format, you just need to convert the input to a this format for matching. Since your field data is already in this format, you will be able to utilize an index for the search.
You are trying to compare a date with a String.
The str_to_date function application is correct, but you are not comparing to a date.
The right way to do it is:
select * from yourTable
where STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= '2013-07-11 00:00:00'
Notice that the date format is YYYY-MM-DD HH:mm:ss (which is MySQL default date format).
Of course, you can also compare to str_to_date results:
... where STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= STR_TO_DATE('Thu, 11 Jul 2013 00:00:00', '%a, %e %b %Y %H:%i:%s')
How do I convert the following format to unix timestamp?
Apr 15 2012 12:00AM
The format I get from DB seems to have AM at the end.
I've tried using the following but it did not work:
CONVERT(DATETIME, Sales.SalesDate, 103) AS DTSALESDATE,
CONVERT(TIMESTAMP, Sales.SalesDate, 103) AS TSSALESDATE
where Sales.SalesDate value is Apr 15 2012 12:00AM
Here's an example of how to convert DATETIME to UNIX timestamp:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))
Here's an example of how to change date format:
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p')),'%m-%d-%Y %h:%i:%p')
Documentation: UNIX_TIMESTAMP, FROM_UNIXTIME
You will certainly have to use both STR_TO_DATE to convert your date to a MySQL standard date format, and UNIX_TIMESTAMP to get the timestamp from it.
Given the format of your date, something like
UNIX_TIMESTAMP(STR_TO_DATE(Sales.SalesDate, '%M %e %Y %h:%i%p'))
Will gives you a valid timestamp. Look the STR_TO_DATE documentation to have more information on the format string.
If you want to create a timestamp as returned by java's Date.getTime() you should multiply by 1000.
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))*1000
Now for a more standard date format use:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('2022-12-14 20:58:00', '%Y-%m-%d %H:%i:%s'))*1000
From http://www.epochconverter.com/
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())
My bad, SELECT unix_timestamp(time) Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD. More on using timestamps with MySQL:
http://www.epochconverter.com/programming/mysql-from-unixtime.php