How to set Order BY to specific variable structure - mysql

Essentially I have a column in my mySQL database that contains time intervals like:
8:00 am - 12:00 pm
The column accepts VARCHAR values.
How can I ORDER BY using this column data in DESC order?
I would like the query to understand am from pm and order in terms of that.

One method is to convert to times:
order by str_to_date(concat('2000-01-01 ', substring_index(str, ' - ', 1)), '%Y-%m-%d %h:%i %p'),
str_to_date(concat('2000-01-01 ', substring_index(str, ' - ', -1)), '%Y-%m-%d %h:%i %p')
The date is irrelevant for the order by, because they are all the same.
Here is a db<>fiddle showing the values produced.

Related

Mysql dat issue while using str to date

I have the data stored as this in my mysql table
http://prntscr.com/tbwiiq
I am trying to get the data based upon the order by clause
SELECT * FROM cw_books order by STR_TO_DATE(BookingTime, '%a, %d %b %Y %T') desc
but it is always giving me wrong order, what the best i can do here
The format that you use is wrong.
Use this:
SELECT *
FROM cw_books
order by STR_TO_DATE(BookingTime, '%m/%d/%Y %h:%i:%s %p') desc
See the demo.

Convert March 2015 to 03/2015 and make into date

I have a column which holds values in the format 'June 2015', 'July 2015', 'March 2014', 'February 2016'.
I need to convert these into the format 06/2015, 07/2015, 03/2014, 02/2016.. but also make them a date so I can sort by this column.
I currently have the following select:
SELECT CONCAT(MONTH(STR_TO_DATE(LEFT(SUBSTRING_INDEX(quotation.quotation_exp_conversion_date, ' ', 1),3), '%b')), '/', SUBSTRING_INDEX(quotation.quotation_exp_conversion_date, ' ', -1)) AS exp_conversion_date
FROM quotation
WHERE quotation.quotation_status = 'LIVE'
ORDER BY exp_conversion_date ASC
There are 2 problems. This query converts the values into the format '6/2015', '7/2015', '3/2014', '2/2016' (without the leading 0 for single digit months), and does not order by correctly as the YEAR is not taken into account - so they are ordered:
'2/2016', '3/2014', '6/2015', '7/2015'
I tried wrapping it all in the DATE() function but that just returned NULL for all values.
Any ideas on how to achieve what I want?
Thanks
STR_TO_DATE works for me with a format of '%M %Y':
mysql> select str_to_date('July 2015', '%M %Y');
+-----------------------------------+
| str_to_date('July 2015', '%M %Y') |
+-----------------------------------+
| 2015-07-00 |
+-----------------------------------+
1 row in set (0.10 sec)
Expand your question if there's some reason you can't do the same.
To turn that into a full query (which I obviously can't test without your tables, so caveat lector):
SELECT DATE_FORMAT(d, '%m/%Y')
FROM
(SELECT str_to_date(quotation_exp_conversion_date, '%M %Y') AS d FROM quotation)
ORDER BY d ASC;

How to display timestamp by using table columns

I am new to one of my project. Previous people saved date and time in two columns in the below format :
For date : 2015-04-15 (date type)
For time : 04:20 PM (varchar)
Now I want to compare the above columns with present date and time.
I tried this below query but it's showing NULL.
SELECT UNIX_TIMESTAMP(
STR_TO_DATE('evnt_endDate Event_EndTime', '%Y-%m-%d %h:%i%p')
)
FROM events
Please could any one tell me how to compare?
Use concat():
SELECT UNIX_TIMESTAMP(STR_TO_DATE(concat(evnt_endDate, ' ', Event_EndTime
), '%Y-%m-%d %h:%i%p')
)
'evnt_endDate Event_EndTime' is a string. You need to reference the fields/columns and concatenate their values like
CONCAT(`evnt_endDate`, ' ', `Event_EndTime`)
The resulting query would then be
SELECT UNIX_TIMESTAMP( STR_TO_DATE( CONCAT(`evnt_endDate`, ' ', `Event_EndTime`), '%Y-%m-%d %h:%i%p') ) from events

How to convert dates in "24-Jun-2013" format to MySQL?

I am trying to convert a large table that has dates in the format Day of Month - Month Abbreviation - Year in a row separated by dashes, like 01-Jan-2014. The closest question I found is this one where the asker ended up doing a search and replace method instead. I could do that too, but I think that surely there is a better way. My code:
SELECT STR_TO_DATE(datecolumn, '%c/%d/%Y') FROM 'Table'
Returns null with dates in the above format but is fine with other date formats. I also tried DATE_FUNCTION and other suggestions I saw online.
This will do it:
SELECT
DATE_FORMAT(
STR_TO_DATE(
REPLACE(
SUBSTRING_INDEX(datecolumn,'-',-2),
'-',
CONCAT(' ', SUBSTRING_INDEX(datecolumn, '-', 1), ', ')
),
'%M %d, %Y'
),
'%Y-%m-%d'
)
FROM `Table`;
Explanation:
We use SUBSTRING_INDEX(datecolumn,'-',-2) to get the last two pieces, like Jan-2014
We replace the - in Jan-2014 with the first piece (using SUBSTRING_INDEX), i.e., 01, but also use CONCAT to add spaces and ,, so we get Jan 01, 2014
We use STR_TO_DATE and tell it the format is %M %d, %Y
We then put it in %Y-%m-%d format with DATE_FORMAT()
Examples:
07-Jan-2014 -> 2014-01-07
9-May-2001 -> 2001-05-09
13-Dec-1978 -> 1978-12-13
Here's a fiddle

How to change date in database

In my Mysql database, i have a set of date.
How can I increase for each and every date in my database by 3 years ?
For example the current date is :
2 January 2001
I want the date to be increased by three years:
2 January 2004
Try this -
UPDATE TABLE set fieldname = DATE_ADD( fieldname, INTERVAL 3 YEAR )
For more information and play part with dates you can check this link :-
function_date-add
Working Fiddle -- http://sqlfiddle.com/#!2/9c669/1
EDIT
This solution updates date type is VARCHAR and structure of date like - 2 January 2001
It will update date to 2 January 2004 by the interval of 3
Although the best way to handle date is use date DATATYPEs(ex timestamp, datetime etc) instead of saving it in VARCHARs
Tested code --
UPDATE date
SET `varchardate`= DATE_FORMAT(DATE_ADD( str_to_date(`varchardate`, '%d %M %Y'), INTERVAL 3 YEAR ) , '%d %M %Y')
Check date_add function, it should do the thing -http://www.w3schools.com/sql/func_date_add.asp
Below query is to update the date column and also changed to date format.
update date1 set dates =
DATE_ADD( (str_to_date(concat(
SUBSTRING_INDEX( dates, ' ', 1 ),'-',
Substring(SUBSTRING_INDEX( SUBSTRING_INDEX( dates, ' ', 2 ) , ' ', -1 ),1,3) ,
'-',
SUBSTRING_INDEX( dates, ' ', -1 )),'%d-%M-%Y')), INTERVAL 3 YEAR )
OR
The second query is simply update the year by 3.
update date1 set dates =
concat( SUBSTRING_INDEX( dates, ' ', 2 ) , ' ',
(SUBSTRING_INDEX( dates, ' ', -1 ) +3) )
SQL Fiddle Demo