Implement where condition varchar field in MySQL - mysql

I have usages table which contains field values like below
id | page_open_time
1, 28 May 2019 08:39:11
2, 12 July 2019 18:39:11
3, 22 December 2019 11:39:11
4, 23 June 2019 12:39:11
Now I would like to get all records where page_open_time is greater than 1 july, 2019. For this, I wrote this query below , but it didnt help. Is there any way, I can perform this ?
SELECT * FROM `usages` where page_open_time > '2019-07-01'

Your dates are not in a valid MySQL DATETIME format, so you need to convert them before comparison, using STR_TO_DATE:
SELECT *
FROM usages
WHERE STR_TO_DATE(page_open_time, '%d %M %Y %H:%i:%s') > '2019-07-01'
Output
id page_open_time
2 12 July 2019 18:39:11
3 22 December 2019 11:39:11
Demo on dbfiddle

Related

How can I sort date perfectly

I was trying to sort the data in-order by date but it doesn't sort perfectly. I don't know if there's an error in the code.
Here's some of the data in character in my database:
March 5, 2016
March 17, 2016
August 9, 2017
April 2, 2016
July 5,2018
January 15, 2019
The query I used is:
SELECT * FROM reporting ORDER BY date ASC
Result are:
April 2, 2016
August 9, 2017
January 15, 2019
July 5,2018
March 5, 2016
March 17, 2016
Expected Result:
March 5, 2016
March 17, 2016
April 2, 2016
August 9, 2017
July 5,2018
January 15, 2019
use str_to_date() to convert your date field from string to datetime
SELECT * FROM reporting ORDER BY str_to_date(`date`,'%M %d, %Y') ASC
use date format
ORDER BY DATE_FORMAT(date, "%F-%d-%Y") as date ASC
You may try this,
For SQL Server:
convert( varchar, [date], 120) will convert your date in yyyy-MM-dd format.
select * from reporting order by convert( varchar, [date], 120) asc
For MySQL:
select * from reporting order by DATE_FORMAT(date, "%Y-%m-%e") asc

Node JS | Grabbing last 7 days of data from the database

I am trying to get last 7 days of data from my database. I have a table called date and I know that I could easily use date >= DATE(NOW()) - INTERVAL 7 DAY but that won't work for me because I have date values like that: Jan 22 2017 16: +0, Jan 22 2017 15: +0, Jan 22 2017 14: +0, Jan 22 2017 13: +0, Jan 22 2017 12: +0 etc. What could be the different way doing it?
Regards
Seems like you store the date as a string.
In this case you could use STR_TO_DATE
SELECT STR_TO_DATE(date,'%M %d %Y %h: +0') AS converted_date, [...] FROM [...] WHERE converted_date >= DATE(NOW()) - INTERVAL 7 DAY
SQL Fiddle

Select query to display Date format as mmm dd yyyy hh:mm am in SQL server 2008

I tried like this
select CONVERT(VARCHAR(20),GETDATE(),100)
result is
____________________
Jun 12 2014 3:10PM
____________________
I need space between time {3:10PM}
SELECT QUERY TO DISPLAY DATE AS SHOWN BELOW
____________________
Jun 12 2014 3:10 PM
____________________
Your datatime is in default format. Example
SELECT CONVERT(CHAR(19), CURRENT_TIMESTAMP, 100)
Returns:
Jun 12 2014 12:21PM
You need to add a space to this just before the 18th character. This is done with stuff:
SELECT STUFF(CONVERT(char(19), CURRENT_TIMESTAMP, 100), 18,0, ' ')
Result:
Jun 12 2014 12:21 PM
Try this:
select REPLACE(REPLACE(CONVERT(VARCHAR(20),GETDATE(),100),'PM',' PM'),'AM',' AM')
This will work in both MySQL and SQL Server, although you really should tag your question with the correct one.

MySQL Show Events in Logical Order

looking to organize the following dates (assuming today is August 24, 2012):
December 1, 2012
November 1, 2012
June 1, 2012
June 30, 2012
In the following way:
November 1, 2012
December 1, 2012
June 30, 2012
June 1, 2012
This is such a way that it shows the events that have NOT yet happened first, and from soonest to furthest away, then show past events, from closest to farthest.
You can assume the table structure is:
ID name event_date
1 Test 1351742400 # All dates are Unix Time
2 Test2 1354338000
SELECT ID, name, event_date
FROM yourtable
ORDER BY event_date < UNIX_TIMESTAMP(), ABS(UNIX_TIMESTAMP() - event_date)
See it working online: sqlfiddle

MySql Query- Date Range within a Date Range

I use mySql 5 and IIS.
I have products, that have a start date field and an end date field.
I need to run a query that will take user entered Start and End dates, and output the number of days that the product ran within the date range.
Example:
Offer1 - July 1 2011 thru July 31 2011
Query - July 1 2011 thru Sept 15 2011
Results = 31
Example:
Offer1 - July 1 2011 thru July 31 2011
Query - July 1 2011 thru July 15 2011
Results = 15
If your products have a start_date and an end_date and your query has a qstart_date and a qend_date, then we want the number of days between:
GREATEST(start_date, qstart_date)
and
LEAST(end_date,qend_date)
. In MySQL I think this looks like
1 + DATEDIFF ( 'd' , GREATEST(start_date, qstart_date) , LEAST(end_date,qend_date) )
And you'll want to ignore negative numbers, replacing them with "0".