Change datatime to date dd/mm/yy - mysql

Im trying to query some data and one of them is a datetime format. I want that is shows dd/mm/yy with no time on it directly form the select. Is this possible?
My query is and the join_date is datetime that i need to be changed to short date:
$query = mysql_query("SELECT id,username,join_date,is_active FROM members",$con)
or trigger_error(mysql_error());
This query goes directy in a Json output array. So i want to convert it directy form the query.

Use the MySQL DATE_FORMAT function for this:
SELECT id, username, DATE_FORMAT(join_date, '%d/%m/%y') AS join_formatted, is_active
FROM members
In this example, the column name for the formatted date will be join_formatted, and its type will be VARCHAR.
The format string returns the date as dd/mm/yy as requested, but I'm personally more comfortable when the date includes the full century. To get the full century, use uppercase Y in the format string: %d/%m/%Y.

try this :
"SELECT id, username, DATE_FORMAT(join_date,'%d/%m/%y'), is_active FROM members"

Use DATE_FORMAT:
SELECT id, username, DATE_FORMAT(join_date, "%d/%m/%y") AS date FROM members;
%Y Year, numeric, four digits
%y Year, numeric (two digits)
For details about date format link

Related

Change Date Format from '-' to '/'

How to update or change date format "%Y-/%m-/%d" to "%Y/%m/%d" in my sql. After refreshing it goes to normal '-' format.
just use select DATE_FORMAT(yourdatecolumn, "%Y/%m/%d")
example
select DATE_FORMAT('2018-08-01', "%Y/%m/%d")
2018/08/01
From https://dev.mysql.com/doc/refman/8.0/en/date-and-time-literals.html :
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. However, it will recognize the (input) format to a date field in any of 'YYYY-MM-DD' or 'YY-MM-DD' or 'YYYY/MM/DD', 'YYYY^MM^DD' and 'YYYY#MM#DD' or even YYYYMMDD.
If you want to display a date in a different format (e.g. your chosen format of YYYY/MM/DD), then you need to change the format in the query string. Consider this example below
I have a table with 3 fields id, Date and Amount. If I run the query
SELECT id, Date, Amount FROM `dtdata` ORDER BY `dtdata`.`id` ASC
I get the output as shown:
If I want to display the date differently (i.e. your required format), I can use the following Query:
SELECT id, DATE_FORMAT(Date, "%Y/%m/%d") AS NewDate, Amount FROM `dtdata`
to get the output as

mysql date comparision is not working unable to find the issue

i have a huge data with dates as string.
column name date1
datatype varchar
the stored data is in this format:14-Mar-2016 05:44:38pm
Now I have split only date from this string like this: 14-03-2016
By using this: DATE_FORMAT(STR_TO_DATE(gr.date1, '%d-%M-%Y'),'%d-%m-%Y')
Now I am trying to compare the date with this query:
SELECT * FROM
( SELECT date1,DATE_FORMAT(STR_TO_DATE(date1, '%d-%M-%Y'),'%d-%m-%Y') as dateFormatted
FROM `grabt` ) as mTbl WHERE mTbl.dateFormatted >= '19-01-2016'
AND mTbl.dateFormatted <= '25-01-2016'
but it is not working what could be the possible error.?
The timestamp string 14-Mar-2016 05:44:38pm can be converted to a datetime using the STR_TO_DATE() along with the format string %d-%b-%Y %r. We can then obtain only the date portion by wrapping that with DATE(). Have a look here for a demo to see that this works.
SELECT *
FROM
(
SELECT DATE(STR_TO_DATE(date1, '%d-%b-%Y %r')) AS dateFormatted
FROM grabt
) AS mTbl
WHERE mTbl.dateFormatted BETWEEN '2016-01-19' AND '2016-01-25'
As Gordon already pointed out, you should ideally be using date types not strings for your date calculations. And by the way, use a valid date string when comparing in your WHERE clause. YYYY-MM-DD is a valid format, e.g. 2016-01-19, but 19-01-2016 is not.
Learn to use the right types for columns. Perhaps you are stuck with someone else's really bad decision to store date/times as strings. Perhaps you cannot change that. But, within a query, use the right types!
SELECT mTbl.*,
LEFT(date1, 10) as FormattedDate -- Is this really necessary?
FROM (SELECT date1,
STR_TO_DATE(LEFT(date1, 10), '%d-%M-%Y') as thedate
FROM `grabt`
) mTbl
WHERE mTbl.thedate >= '2016-01-19' AND
mTbl.thedate <= '2016-01-25';
This will do the comparison as dates not as strings.

Update DATE in tables stored as VARCHAR after conversion

I imported data from Excel and brought dates over in VARCHAR format as 03/24/2017 because the dates were not saving. So I used VARCHAR for expediency. Now I am paying the price.
I am trying to use the STR_TO_DATE function to convert so that I can UPDATE a new column (defined as DATE) in the row.
When I use the following it looks promising, but only the month/day convert properly, the year becomes 2020 as shown below:
date_order $conv_date_order
3/24/2017 2020-03-24
SELECT date_order, STR_TO_DATE(date_order, '%m/%d/%y') AS $conv_date_order,
date_shipped, STR_TO_DATE(date_shipped, '%m/%d/%y') AS $conv_date_shipped,
date_need, STR_TO_DATE(date_need, '%m/%d/%y') AS $conv_date_need
FROM Orders
WHERE Orders.id = $id;
You need to use a capital Y in your '%m/%d/%Y' format string to indicate a four digit year. The lowercase y indicates a two digit year.

Mysql data and convert

I have table that contain a string field with data.
The date is in this format: dd/mm/yyyy
I need to show the max date.
I try some thing but without success
Select max(to_date) from payment
Thanks
Use the STR_TO_DATE() function:
SELECT MAX(STR_TO_DATE(to_date, '%d/%m%Y')) FROM payment;
you must convert your string to a date,
because now you are doing the max over a string not a date
SELECT MAX(STR_TO_DATE(to_date, '%d/%m/%Y'))
FROM payment

SQL Change date format from yyyy-mm-dd to dd-mm-yyyy

I have created MySQL table :
CREATE TABLE EMP(
EMPID INTEGER NOT NULL (5),
SURNAME VARCHAR(25),
SAL INTEGER(5),
JON VARCHAR(25),
START_DATE DATE,
END_DATE DATE,
DEPNO INTEGER(5)
);
with following records:
INSERT INTO EMP
(EMPID,SURNAME,SALARY,JOB,START_DATE,END_DATE,DEPNO)
VALUES
('1','Vorosila','500000','COO','20150101',null,'1');
however I need to change date format from 2015 01 01 to 01 01 2015
Can anybody show me or tell me how to do that ?
THANK YOU IN ADVANCE
DATE values do not have a "format", they are objects that represent instants in time (or entire days, but still independent of formatting).
Formats are applied on input and output, so you just need to apply the correct format, which you can find in the MySQL manual, to the SELECT statement.
You cannot change the default date format in mysql.
I once hoped for the default date to be editable so I wouldn't have to jump through these hoops to get the date I actually wanted, mysql even has a date format system variable, but it is unused. Date Format Mysql - link
What you should really do is store it as the default format Year-Month-Date and then convert it on select.
The first thing I'd suggest is having your date columns as date types, which would give your dates the following format '2015-01-01'.
If you do this then you can use DATE_FORMAT - link - the second value in the DATE_FORMAT function allows you to customise the returned date, and there are many different thing you can do with this if you look at the link:
SELECT
DATE_FORMAT(`START_DATE`,'%d-%m-%Y')
AS `START_DATE`
FROM ...
The other option you have is to store your dates in the format that you already want as a char or varchar column.
HOWEVER, as should be obvious, this column will not be treated as storing dates, and so will not give you the correct comparisons in a where clause when using > < BETWEEN or the correct ordering in an order by clause. It is after all just a string of numbers in this case.
However you can then use STR_TO_DATE - link if you did need to use a where or order by on this column to change it back to a date within the query - in this case the second value is the custom format of your 'dates' in the column. Keep in mind with a where you will need to compare it with the correct mysql format as shown below:
SELECT
`START_DATE`
FROM table
WHERE STR_TO_DATE(`START_DATE`,'%d-%m-%Y') BETWEEN '2015-01-01' and '2016-01-01'
In MySQL you can change the format of a date using DATE_FORMAT method which is similar to to_char in Oracle.
DATE_FORMAT(SYSDATE(), '%DD-%MM-%YYYY');
For more information about specifiers check this thread http://www.sqlines.com/oracle-to-mysql/to_char_datetime
You can do what you probably want by creating a view and referring to that instead of the (underlying) table.
CREATE VIEW emp_view AS
SELECT empid,
surname,
sal,
jon,
date_format(start_date, '%d-%m-%Y') as start_date,
date_format(end_date, '%d-%m-%Y') as end_date,
depno
FROM emp;
Note that this changes the type of the date columns to varchar, so comparisons will no longer work as expected:
SELECT * FROM emp_view WHERE start_date > '01-12-1924'; // fails!