Change MYSQL date format - mysql

I have a table in MYSQL of which 3 columns have dates and they are formatted in the not desired way.
Currently I have: mm/dd/yyyy and I want to change those dates into dd/mm/yyyy.
Table name is Vehicles and the columns names are:
CRTD
INSR
SERD

Your current datatype for your column is not date right? you need to convert it to date first using STR_TO_DATE() and convert back to string
SELECT DATE_FORMAT(STR_TO_DATE(colName, '%c/%d/%Y'), '%d/%c/%Y')
FROM table1
SQLFiddle Demo

try this:
select date_format(curdate(), '%d/%m/%Y');
In you case you have to use this query.If all three columns are of datetime type
select date_format(CRTD, '%d/%m/%Y'),
date_format(INSR, '%d/%m/%Y'),
date_format(SERD, '%d/%m/%Y')
From yourTable

You can use the date_format() function to format your dates any way you want.
If you want to change the format for every date on any database you work with, you should change the value of the date_format system variable.

UPDATE Vehicles SET yourdatecolumn=DATE_FORMAT(STR_TO_DATE(yourdatecolumn, '%c-%d-%Y'), '%d-%c-%Y')

Related

Can't change date format SQL

I have the current date format [DD_MM_YYY] in a column on my table and would like to change it to [YYYY_MM_DD]. Can't seem to figure out the correct syntax.
This one should do for date fields:
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d');
You can use the convert function in sql.
SELECT CONVERT (data_type(length)),Date, DateFormatCode)
To convert in this format [YYYY_MM_DD] if the date format is in varchar:
Select CONVERT(varchar,column_name,23)
If you are using Exasol, you can use to_date():
select to_date(datecoll, 'DD_MM_YYYY')
I would recommend leaving this as a date. But if you want to convert this back to a string:
select to_char(to_date(datecoll, 'DD_MM_YYYY'), 'YYYY-MM-DD')

How can I convert date format from YYYY-MM-DD to YYYY-MM in sql

I have a specific date in the format of YYYY-MM-DD but want to convert it to YYYY-MM format in a table. I want this change to apply to the full table. can anyone help me out, please?
Thank you.
Assuming that you have a date datatype or the-like (datetime, timestamp), you can use date_format() to represent your date in the target format:
date_format(mydate, '%Y-%m')
This returns a string in the target format. It does not make sense to convert your date column to a string though. Keep that column as it is, and maybe use a computed column to automatically derived the string representation you want:
create table mytable (
...
mydate date,
mynewcol varchar(7) as (date_format(mydate, '%Y-%m'))
)
You can use DATE_FORMAT function of MySQL, for more information please visit
SELECT DATE_FORMAT("2017-06-15", "%Y-%m");

Changing a string date to date datatype

I am trying to sort a table using the column Date. It is sorted upside down i.e the date is in descending format.
However, the column Date is in text format: '31-June-2008'
How could I sort this column?
Tried this code to alter but didn't work
alter table bajaj1 modify column Date date;
Your problem is that your dates are not in a valid date format so they are being sorted as strings. So for example, 31-June-2008 is "before" 9-June-2008. To sort them correctly you need to convert them to dates, which you can do using STR_TO_DATE:
STR_TO_DATE(Date, '%e-%M-%Y')
Note I've assumed when the day of the month is less than 10, it will look like e.g. 4-March-2001. If it is like 04-March-2001 you should change %e to %d.
In a query you could use this as
SELECT *
FROM yourtable
ORDER BY STR_TO_DATE(Date, '%e-%M-%Y')
If you wanted to alter your table structure, it's probably simplest to add a generated column:
ALTER TABLE yourtable ADD sdate DATE AS (STR_TO_DATE(Date, '%e-%M-%Y'))
Then you can sort on sdate instead of Date.
Demo on dbfiddle

SQL Comparing Dates

in a database table I have made a date attribute but I have set it's type to varchar and not Date.
My question is, will I still be able to compare such dates in a SQL Query?
Dates in my DB are stored in this format:
dd/mm/yyyy hh:mm:ss
I have to do a SQL Query in PHP that looks something like this:
SELECT *
FROM DBtable
WHERE DBname='$name' AND date>='01/01/2015' AND date<='01/09/2015';
I would appreciate an example how to do this.
Thank you for your help.
You'll need to convert/cast to compare:
SELECT *
FROM DBtable
WHERE DBname='$name'
AND CAST(date AS DATETIME) >='2015-01-01'
AND CAST(date AS DATETIME)<='2015-01-09'
;
Much better to store values as the appropriate data types to avoid this inefficiency. You could also use DATE instead of DATETIME if you want to compare without the time component. Syntax and available datatypes vary by database, so the above may need adjustment.
Update: Since you're using MySQL, you can use the following:
SELECT *
FROM DBtable
WHERE DBname='$name'
AND STR_TO_DATE(`date`, '%d/%c/%Y') >= '2015-01-01'
AND STR_TO_DATE(`date`, '%d/%c/%Y') <= '2015-01-09'
;
Yes you can cast a Varchar to a Date. Here is an example:
SELECT
CAST(date_column AS DATETIME)
FROM
TABLE_NAME
In your case it might look like:
SELECT *
FROM DBtable
WHERE DBname='$name'
AND CAST(date AS DATETIME) >='01/01/2015'
AND CAST(date AS DATETIME) <='01/09/2015';
You can cast or convert a varchar to a date or datetime before you do any comparisons.
But you'd have to do it every single time you compare the date to something. That's because the following comparisons are all true if you compare them as varchar:
'2/1/2015' > '1/5/2016'
'25/1/2015' > '15/2/2015'
'11/1/2015' < '3/1/2015'
You'll also need to convert if you want to pull out some time-based aspect of the dates, such as any records where the hour was before 8:00 AM. There is no easy way to do that if your date is a varchar.
And that assumes that the value in your database can always be parsed into a date! If an empty string or some other kind of data gets in there, CONVERT(datetime, MyColumn) will fail.
So I would strongly recommend that you change your column to be a date or datetime. It will make your life much easier.

How do you change date format in mysql?

How do you change date format from yy-mm-dd to day-month-year?
I have tried this:
UPDATE stock
SET date_in = DATE(STR_TO_DATE(date_in, '%d/%m/%Y))
WHERE DATE(STR_TO_DATE(date_in, '%d/%m/%Y') <> '00-00-0000';
But it is giving me an error.
You store dates in database in one of the date formats (date,datetime,timestamp)
When you want to format them for view, either use your language, which surly has a date formatting functionality, or use the intrinsic mysql date_format in a SELECT statment