I have a varchar dating field that reads as so - xx/xx/xxxx
I need to return the max as xx/xx/xxxx AM(or PM)
I can't figure out how to get it to return the max correctly while including AM/PM
I have been playing around with
SELECT DATE_FORMAT(max(STR_TO_DATE(pg_date_, '%c/%e/%Y %H:%i')), '%Y-%m-%d %H:%m:%s') from cas_compliance.failedrefunds2
I can't get to return quite the way I need it.
Thanks!
To convert xx/xx/xxxx to xx/xx/xxxx AM(or PM), you can try the following:
SELECT DATE_FORMAT(MAX(STR_TO_DATE(pg_date_,
'%c/%e/%Y %H:%i')
),
'%c/%e/%Y %r')
FROM cas_compliance.failedrefunds2
Details:
%r Time in 12 hour AM or PM format (hh:mm:ss AM/PM)
%H Hour (00 to 23)
%i Minutes (00 to 59)
%c Numeric month name (0 to 12)
%e Day of the month as a numeric value (0 to 31)
%Y Year as a numeric, 4-digit value
select DATE_FORMAT(now(), "%d-%m-%y %r");
Convert your date using above formating will solve problem.
Related
I have a column in a MySQL database that has dates in the form:
Tue Oct 25 2016. I am trying to get it in the form 10/25/2016.
I did some research and tried this:
SELECT DATE_FORMAT(Date, '%d/%m/%Y') FROM table;
But it is returning null
Any help would be greatly appreciated.
Firstly, you will need to convert your date string to MySQL date format ('YYYY-MM-DD'), using STR_TO_DATE function. To convert from string, we have to specify the current format of the date string. In your case, it is '%a %b %d %Y'. Note that the % character is required before format specifier characters.
Details:
%a Abbreviated weekday name (Sun to Sat)
%b Abbreviated month name (Jan to Dec)
%d Day of the month as a numeric value (01 to 31)
%Y Year as a numeric, 4-digit value
Now, you can utilize DATE_FORMAT function to convert the MySQL date into the desired date string format. In your case, it will be: '%m/%d/%Y'
Details:
%d Day of the month as a numeric value (01 to 31)
%m Month name as a numeric value (00 to 12)
%Y Year as a numeric, 4-digit value
Try the following query:
SELECT DATE_FORMAT(STR_TO_DATE(Date, '%a %b %d %Y'), '%m/%d/%Y')
FROM table;
Complete list of available format specifiers can be seen at: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
I am new to mysql. I need to insert the string '2014-07-10 13:33:33' into the table column which has datetime datatype.
I gave like this,
SELECT STR_TO_DATE('2014-07-09 23:30:00', '%Y/%m/%d %h:%m:%s');
But i didn't not give the result.
How to do this?
Minutes is %i, not %m and 24-hour format is %H, not %h:
SELECT STR_TO_DATE('2014-07-09 23:30:00', '%Y-%m-%d %H:%i:%s');
Shouldnt it be
SELECT STR_TO_DATE('2014-07-09 23:30:00', '%Y-%m-%d %H:%i:%s');
%Y for year numeric, four digits
%m for month numeric
%d for Day of the month, numeric
%H for 24 hours
%i for minutes
%s for seconds
I've tried using
FROM_UNIXTIME(`date`)
and got a yyyy/mm/dd hour
how can I reverse it, that is have it as
hh:mm:secs mm/dd/yyyy
Thanks
Add specifiers to FROM_UNIXTIME or use DATE_FORMAT:
Specifiers
%T Time, 24-hour (hh:mm:ss)
%m Month, numeric (00..12)
%d Day of the month, numeric (00..31)
%Y Year, numeric, four digits
SELECT DATE_FORMAT(FROM_UNIXTIME(`date`), '%T %m/%d/%Y)
Or
SELECT FROM_UNIXTIME(`date`, '%T %m/%d/%Y')
Use DATE_FORMAT
DATE_FORMAT(FROM_UNIXTIME(`date`), "%T %m/%d/%Y")
This will return hh:mm:ss dd/mm/YYYY from unix time stored in database.
you have to define your custom date/time format. please have a look at:
http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html#function_from-unixtime
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(), '%h:%i:%s %M/%D/%Y');
I have to compare date's in mysql, for example:
select col1,col2 from table where date <= '2011-12-24' (present date)
But this gives an output of all the dates less then '2011-12-24' only.
I suspect your problem is that your date column is a timestamp, but you're comparing it to a date. When this is done, your 2011-12-24 is converted to 2011-12-24 00:00:00.0000 and hence anything that has 2011-12-24 with a reasonable time is after this point. In your situation, I'd use
select col1,col2 from table where my_date < '2011-12-25' (present date + 1 day)
or, if you insist on using <= then
select col1,col2 from table where date(my_date) <= '2011-12-24' (present date)
Well, now try the following.
SELECT col1,col2 FROM table
WHERE date <= STR_TO_DATE('2011-12-24 00:00:00', '%Y-%m-%d %H:%i:%s');
This would better help you, if you need the TimeStamp portion in comparision.
STR_TO_DATE(str,format) is the inverse of the DATE_FORMAT() function.
STR_TO_DATE() returns a DATETIME value.
The following specifiers may be used in the format string.
The '%' character is required before format specifier characters.
Specifier Description
%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%c Month, numeric (0..12)
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, ?-)
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00..53), where Sunday is the first day of the week
%u Week (00..53), where Monday is the first day of the week
%V Week (01..53), where Sunday is the first day of the week; used with %X
%v Week (01..53), where Monday is the first day of the week; used with %x
%W Weekday name (Sunday..Saturday)
%w Day of the week (0=Sunday..6=Saturday)
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal '%' character
%x x, for any 'x' not listed above
this works... jst need to convert the column to date format
select col1,col2 from table where DATE_FORMAT(date, '%Y%m%d') <= '2011-12-24'
I have rows in a table that I need to order by date. Easy enough, use the date datatype to store them, ORDER BY date blah blah.
However, I'm trying to get these rows, and easily convert the date to MonthName, Year
If I had stored a row's date as 2011-11-16, I would want to extract this (using PHP) and print out:
November, 2011
Try this
SELECT DATE_FORMAT(`date`,'%M %Y') AS showdate FROM table
You can get date year and monthName formate using the following query.
SELECT DATE_FORMAT("2017-06-15", "%Y %M") as 'Date';
If you need other formats apply following combinations.
Syntax: DATE_FORMAT(date, format_mask)
Format Description
%a Abbreviated weekday name (Sun to Sat)
%b Abbreviated month name (Jan to Dec)
%c Numeric month name (0 to 12)
%D Day of the month as a numeric value, followed by suffix (1st, 2nd, 3rd, ...)
%d Day of the month as a numeric value (01 to 31)
%e Day of the month as a numeric value (0 to 31)
%f Microseconds (000000 to 999999)
%H Hour (00 to 23)
%h Hour (00 to 12)
%I Hour (00 to 12)
%i Minutes (00 to 59)
%j Day of the year (001 to 366)
%k Hour (0 to 23)
%l Hour (1 to 12)
%M Month name in full (January to December)
%m Month name as a numeric value (00 to 12)
%p AM or PM
%r Time in 12 hour AM or PM format (hh:mm:ss AM/PM)
%S Seconds (00 to 59)
%s Seconds (00 to 59)
%T Time in 24 hour format (hh:mm:ss)
%U Week where Sunday is the first day of the week (00 to 53)
%u Week where Monday is the first day of the week (00 to 53)
%V Week where Sunday is the first day of the week (01 to 53). Used with %X
%v Week where Monday is the first day of the week (01 to 53). Used with %X
%W Weekday name in full (Sunday to Saturday)
%w Day of the week where Sunday=0 and Saturday=6
%X Year for the week where Sunday is the first day of the week. Used with %V
%x Year for the week where Monday is the first day of the week. Used with %V
%Y Year as a numeric, 4-digit value
%y Year as a numeric, 2-digit value
SELECT DATE_FORMAT(date_col, '%M, %Y')
FROM tbl
-- WHERE ??
ORDER BY date_col;
The manual knows more about that.
Try this in php:
<?=format_date($column_name['date'],"M, Y")?>
Once you get date in mysql, use this in php
$date = mysql_result(mysql_query("SELECT date FROM table"),0,0);
echo date("F, Y", strtotime($date));
try this
$sql = "SELECT DATE_FORMAT(`date`,\'%M %Y\') AS showdate FROM `allowances`";
In SQL you could get
SELECT year(DBDate), month(DBDate) FROM dates ORDER BY DBDate