select from database where date < today mysql - mysql

I have mysql database which contain date like this value
22 Jan 2019 11:03
I want to select all records that have values less than today , so I wrote this query
select username from radcheck
where DATE_FORMAT(STR_TO_DATE(value,'%d %M %Y') ,'%d-%m-%Y') < DATE_FORMAT(NOW(), '%d-%m-%Y')
but this query return all records even those not expired .
where is my problem

SELECT username
FROM radcheck
WHERE STR_TO_DATE(`value`,'%d %b %Y %H:%i') < CURRENT_DATE

Related

SQL get dates later than specified date

I'm working on a database that stores a date column in a human-readable format. This seems to make it tricky to work out rows where a given date is later than.
The format is stored like 20 November, 2018
I'm trying to return all rows where the collection_date is later than 5 November, 2018
I've tried the following query which throws an error.
SELECT *
FROM `orders`
WHERE collection_date >= CONVERT(datetime, '20181105')
This throws the following error message:
Here's my DB info:
UPDATE 1:
I'm trying the following query. None of the previous queries have worked so far, all producing 0 rows:
SELECT *
FROM `orders`
WHERE STR_TO_DATE('collection_date', '%d %M, %Y') >= 2018-11-05
This also does not work
Actually... you have to apply STR_TO_DATE on the collection_date column because that is where the human readable dates are. As for the input date, just specify it in yyyy-mm-dd format:
SELECT *
FROM `orders`
WHERE STR_TO_DATE(collection_date, '%e %M, %Y') >= '2018-11-05'
In MySQL, you want STR_TO_DATE():
SELECT o.*
FROM `orders` o
WHERE collection_date >= Str_To_Date(datetime, '%e %M, %Y');
CONVERT() with this syntax is a SQL Server function.
The value comes first in the convert() function
WHERE collection_date >= CONVERT('2018-11-05', date)
Demo
You can try this using STR_TO_DATE() function
SELECT *
FROM `orders`
WHERE collection_date >= STR_TO_DATE('20181105', '%Y%m%d')
This query worked for me:
SELECT *
FROM orders
WHERE STR_TO_DATE(collection_date, '%d %M, %Y') >= '2018-11-05'
AND collection_time = '4:00 PM'
ORDER BY `orders`.`collection_date` DESC

MySQL - BETWEEN clause not working properly with DATETIME

I have a database where it is stored the date of creation of the record. The problem is that when I try to SELECT all records within 1 year, I get no rows returned.
I created a SQLFiddle to illustrate what I have and what I'm trying to do: http://sqlfiddle.com/#!9/33a32b/8
SELECT aux.description, DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS `formatted_date`
FROM aux
WHERE aux.date_creation BETWEEN 'CURDATE() - INTERVAL 1 YEAR' AND 'CURDATE()'
ORDER BY `formatted_date` DESC;
This is your query:
SELECT aux.description, DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS `formatted_date`
FROM aux
WHERE aux.date_creation BETWEEN 'CURDATE() - INTERVAL 1 YEAR' AND 'CURDATE()'
ORDER BY `formatted_date` DESC;
The single quotes are incorrect. You have a string constant, not a date expression. Presumably, date_creation is in the past, so all you need is:
SELECT aux.description,
DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS formatted_date
FROM aux
WHERE aux.date_creation >= CURDATE() - INTERVAL 1 YEAR
ORDER BY aux.date_creation DESC;
Note that I also changed the ORDER BY. Normally, one wants Jan 2nd to follow Jan 1st, not Feb 1st.

finding date and time of last 5 days

I am trying to find out the date and time of last 5 days from the database,the data inserted into the table is in the form of date("Y-m-d h:i:s");
Now when i try retrieving it the values do not show up until the time from the table is removed
My Query is :
SELECT Key,DATE_FORMAT(lastUpdated, '%D %M %Y')
FROM table
WHERE lastUpdated Between NOW() - INTERVAL 5 DAY AND NOW()
ORDER BY Key DESC;
I believe you want to just compare the DATE() of the lastUpdated
SELECT Key,DATE_FORMAT(lastUpdated, '%D %M %Y')
FROM table
WHERE DATE(lastUpdated) Between NOW() - INTERVAL 5 DAY AND NOW()
ORDER BY Key DESC;

Order dates in MySQL with this format "01 August 2013"

how to order dates in ASC,
Dates are in the following format
01 January 2013
13 August 2013
27 June 2013
I've tried this query
SELECT * FROM table ORDER BY TIMESTAMP(date) ASC
and this query
SELECT * FROM property_rent ORDER BY UNIX_TIMESTAMP(date) ASC
Using the str_to_date function:
SELECT * FROM table ORDER BY str_to_date(date, 'your format here')
e.g.
SELECT * FROM table ORDER BY str_to_date(date, '%d %M %Y')
or similar.

Is subquery the solution to this?

Here's the table structure and some sample data:
pID.....month.....year
27 .....3 .....2008
27 .....12 .....2012
31 .....6 .....2008
99 .....1 .....2006
42 .....1 .....2009
pID is the practiceID and month and year represent the date period they've entered data for. I need to grab the number of practices that have entered data for the first time in Oct 2012, Nov 2012, Dec 2012 and so on.
I tried the following query for Oct 2012:
SELECT *
FROM
IPIPKDIS
where
practiceID NOT IN (
SELECT practiceID
from
IPIPKDIS
where
year < 2012 and month < 10
)
and year = 2012
and month = 10
and measureCatRecID = 2
ORDER BY year, month;
but it's grabbing months and year less than 10/2012.
If I run the queries isolated (not as subquery) they both work fine.
Any ideas?
This summary query will yield the first (smallest) date in the table for each value of practiceID.
SELECT practiceID,
MIN(STR_TO_DATE( CONCAT(year, ' ', month), '%Y %m')) first_date
FROM IPIPKDIS
GROUP BY practiceID
If you want to retrieve then the whole row for the first reported month, you'd do a nested query like this:
SELECT *
FROM IPIPKDIS I
JOIN (
SELECT practiceID,
MIN(STR_TO_DATE( CONCAT(year, ' ', month), '%Y %m')) first_date
FROM IPIPKDIS
GROUP BY practiceID
) first ON ( first.practiceID = I.practiceID
AND STR_TO_DATE( CONCAT(I.year, ' ', I.month), '%Y %m') = first.first_date)
The trick to the second query is to use the JOIN to extract just the first-month rows from your table. We use date arithmetic to do the date comparisons.