Adding a date from one of my tables to a date_add - mysql

Is there anyway of adding a date from my tables to an date_add? For example, in the code, i want to change 2009-05-25 to a date that exists from a table. Thanks in advance.
select date_format( date_add("2009-05-25",interval 2500 day),"%d - %m - %Y" )
as fire_date;

Just use the column name instead of "2009-05-25"
select date_format( date_add(my_column_name,interval 2500 day),"%d - %m - %Y" ) as fire_date;

Related

Sort on string as date on MySql

I have CHAR strings stored in the database field in the format mm/dd/yyyy. Such as
2/26/2022
2/19/2022
2/12/2022
2/5/2022
12/31/2021
12/18/2021
11/27/2021
I need to sort them as shown according to the "date" without changing the declaration.
The post at MySQL date format DD/MM/YYYY select query? suggested using ORDER BY STR_TO_DATE(datestring, '%d/%m/%Y')
My MySQL statement looks like this:
SELECT stringdate
FROM mytable
WHERE product = '#myproduct#'
ORDER BY STR_TO_DATE(stringdate, '%m/%d/%y') DESC
However, the result is not sorted properly. Instead of the desired order as shown above, it is showing like this:
12/31/2021
12/18/2021
11/27/2021
2/26/2022
2/19/2022
2/12/2022
2/5/2022
It seems that the year is being ignored. How can I sort this without actually changing the database field declaration?
Thanks in advance.
2/5/2022 is month and day without leading zeros, and four digit year. The format string you have specified is -
%m - Month, numeric (00..12)
%d - Day of the month, numeric (00..31)
%y - Year, numeric (two digits)
SELECT stringdate
FROM mytable
WHERE product = '#myproduct#'
ORDER BY STR_TO_DATE(stringdate, '%c/%e/%Y') DESC
%c - Month, numeric (0..12)
%e - Day of the month, numeric (0..31)
%Y - Year, numeric, four digits
Executing the following query shows the difference in the converted dates -
SELECT
stringdate,
STR_TO_DATE(stringdate, '%m/%d/%y'),
STR_TO_DATE(stringdate, '%c/%e/%Y')
FROM mytable
WHERE product = '#myproduct#'
ORDER BY STR_TO_DATE(stringdate, '%c/%e/%Y') DESC
db<>fiddle
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
%y is the two-digit year code. So you are sorting them all as '20'
%Y is the four-digit year code.
See reference for the date format codes here: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
I recommend you use the DATE data type instead of CHAR.

How to convert timestamp from MySQL table to human readable form

Any idea how to format a timestamp from a MySQL table inside a query to a human readable form?
Example:
{ts '1978-01-16 00:00:00'}
Needed output:
16-01-1978
I have tried:
select DATE_FORMAT(BIRTHDAY, '%d-%m-%Y') FROM cs_user
with no success:
[Table (rows 10 columns DATE_FORMAT(BIRTHDAY, '%D-%M-%Y')): [DATE_FORMAT(BIRTHDAY, '%D-%M-%Y'): coldfusion.sql.QueryColumn#1077e6ed] ] is not indexable by DATE_FORMAT(BIRTHDAY
My BIRTHDAY column is created as date in the MySQL table.
Take the alias name for the field and try it.
select DATE_FORMAT(BIRTHDAY, '%d-%m-%Y') as some_alias_name FROM cs_user
check, this for an example related to this.
after DATE_FORMAT in a bracket first parameter will be column of the table, then the date format,
Try this
select DATE_FORMAT(date, '%d-%m-%Y') as BIRTHDATE FROM cs_user
find the below link for all the date format which mysql supports.
http://www.w3schools.com/sql/func_date_format.asp
Try this:
SELECT DATE_FORMAT( FROM_UNIXTIME( table.column ) , '%d - %m - %Y' ) FROM table

How to use function with Table

I created a table in which i used timestamp column which tells the record is updated on that time and date.
create table age_info (age tinyint not null,created_on timestamp not null );
But i want to change display like this.select date_format(Now() ,'%W, %e %M %Y # %r');
Tuesday, 31 July 2015 # 02:32:16 PM
But don't know how to do it. When i insert values
insert into age_info(age) values (19);
in the table it show like this.
age created_on
19 2015-07-31 18:55:01
I Don't know how to use this function date_format with timestamp column to show like that format.
I think this will help you to solve the answer
SELECT DATE_FORMAT(`created_on` , '%W, %e %b %Y # %r') FROM `age_info`;
To insert,
INSERT INTO `age_info` (`age`) select DATE_FORMAT(now() , '%W %e, %b %Y # %r')created_on from age_info
Note that, You need to change the table field to varchar.
You can keep the format you want by changing your column definition to a text column. Is that what you really want? You'll lose all the benefits a timestamp column offers (easy date and time calculations, etc).
You can't change the datetime column type to store the data as you want, it changes the meaning of the datetime and no advantage of it, although you can use it to select the format whatever you want using date_format() function.

How to convert and use varchar data type as a datetime in mysql

Hello all,
This is the format of my my-sql data type "rdate".
Apr 1 2011 01:13:00:000PM
I want to use the order by rdate and i can't make it right order as the data type of rdate is varchar, So i want to convert it to date time , But no success.
I am trying to use date_format(str_to_date(rdate, '%m/%d/%Y'), '%Y%m');
Thanks
Mypixel
Try doing:
ORDER BY str_to_date(rdate,'%M %d %Y %h:%i:%s')
From the docs:
Your Date is in the Following format:
%M Month name (January..December)
%d Day of the month, numeric (00..31)
%Y Year, numeric, four digits
...
You have to tell str_to_date the format that your string is in. This means the way the specific parts of the date are displayed, spaces, etc.
sqlfiddle demo
In your str_to_date function call, you need to specify what the format IS, not what you want it to be. Try this:
str_to_date(rdate, '%M %d %Y %h:%i:%s'));
UPDATE table SET rdate=str_to_date(rdate,'%M %d %Y %h:%i:%s')
Just convert your column for good to datetime.

MYSQL: How do I set a date (makedate?) with month, day, and year

I have three columns, y, m, and d (year, month, and day) and want to store this as a date.
What function would I use on mySQL to do this?
Apparently makedate uses year and day of year (see below), but I have month.
I know I can use STR_TO_DATE(str,format), by constructing the string from (y,m,d), but I would guess there is an easier way to do it.
REFERENCES
MAKEDATE(year,dayofyear)
Returns a date, given year and day-of-year values. dayofyear must be greater than 0 or the result is NULL.
I believe you can use a string in the proper format:
UPDATE table SET my_date = '2009-12-31';
Edit: Yeah you can, just verified it in MySQL 5.1.
It isn't high on readability but the following would work:
SELECT'1900-01-01' + INTERVAL y-1900 YEAR + INTERVAL m-1 MONTH + INTERVAL d-1
DAY FROM ...
Not sure that this any more efficient than using CONCAT and STR_TO_DATE.
i hope this wil work out..
str_to_date( concat( year( curdate( ) ) , '-', month( a.dob ) , '-', day( a.dob ) ) , '%Y-%m-%d' )
Where a.dob is my column's name which has DATE