Convert 2021-1-01 to 2021-01-01 in SQL (YYYY-M-DD TO YYYY-MM-DD) - mysql

As title.
There is a way to convert a date from YYYY-M-DD to YYYY-MM-DD?
In db I have 2021-1-01 but I need to get 2021-01-01.
TY
EDIT
I have a value 2021-1-01 and i need to insert it in a db like date 2021-01-01, maybe before I not was clear.

If you have '2021-1-01', then you do not have a date. You have a string. That is a problem with your data model. You should fix the data to store dates using appropriate types -- which are not strings.
MySQL is pretty smart about converting dates, so you can just use:
select date(string_as_date_col)
You can change the type of the column in the table using:
alter table t modify column string_as_date_column date
Here is a db<>fiddle.

Related

How to insert date in a new table in YYYY-MM-DD' format?

CREATE TABLE ORDERS (
ORD_NUM NUMERIC(6,0) NOT NULL PRIMARY KEY,
ORD_AMOUNT NUMERIC(12,2) NOT NULL,
ORD_DATE DATE NOT NULL,
INSERT INTO ORDERS VALUES('200100', '1000.00', '08/01/2008');
INSERT INTO ORDERS VALUES('200110', '3000.00', '04/15/2008');
INSERT INTO ORDERS VALUES('200107', '4500.00', '08/30/2008');
Since I have large number of rows with date in the above format, how can I convert the into yyyy-mm-dd format?
The below solution does not work as the values are not inserted into the ORDERS table.
So, nothing to update.
UPDATE ORDERS
SET ORD_DATE = DATE_FORMAT(ORD_DATE, '%Y-%m-%d');
Executing the code gives error, which I learned to be due to the date format, which MySQL does not allow.
The question misunderstands how dates work in SQL. The Date type does not have ANY human-readable format at all. The values in this column will be stored as binary data, which has significant benefits over string formats for memory/storage use, date math, and indexing.
Now we insert a value like '08/01/2008' into a date column. I will interpret this to mean August 1st based on the other values in the question (this isn't a universal or even majority interpretation!). This value provides the month first, then the day, then the year... but MySQL will not store it that way and does not preserve the original format.
Therefore it makes no sense at all to UPDATE the column to set a specific format. You can't do it, because dates are not stored in a way that preserves any write-able format.
What you can do is format the value at output time, as part of a SELECT query, to use whatever format you need. Additionally, you can use the Str_To_Date() function to control how string values will be interpreted when creating or comparing to native SQL dates.
One thing to keep in mind: thanks to cultural/internationalization issues, converting dates (and numbers!) to and from strings is much slower and more error-prone for a computer than you likely expect. It's something to avoid. Therefore, converting to the native date format early, and leaving it that way as long as possible, is usually the best option.
You just need to convert the string to a date in your insert statements:
INSERT INTO ORDERS VALUES('200100', '1000.00', TO_DATE('08/01/2008', 'mm/dd/yyyy');

Is it possible to change the structure of a column in a view from varchar to date

I currently have a column ('kWhdate') in a view ('appdatatest') that takes a timestamp column and converts it to a date.
date_format(`t`.`value_timestamp`,'%d/%m/%y%y') AS `kWhDate`
The above is part of the Create View query. It is currently stored as VARCHAR(10). I would like to convert it to a Date. I have tried the following:
ALTER VIEW appdatatest MODIFY appdatatest.kWhDate datetime
SELECT STR_TO_DATE(appdatatest.kWhDate, Date) FROM appdatatest
I have looked at this question Is it possible to change the datatype of a column in a view? but it gives an answer for SQL Server not MYSQL
If possible I could convert in the original CREATE VIEW query
Thanks
You can convert a timestamp to a date with date().
SELECT date(kwhdate) kwhdate
FROM appdatatest;

Changing Date Format in SQL Table

I currently have a Release_Date(Date) in my Songs table. I been trying to change the date format. The current format is yyyy-mm-dd. I want mm/dd/yyyy.
Error: Invalid Date value.
Release_Date is stored in the database as a Date, not as a string, so you don't need to call the str_to_date function. You are getting an error because you are calling the str_to_date function on something that is already a date, not a string.
Furthermore, as it is a date, you can't update that field to a string value. You would have to create a new column defined as a string and store the date there.
However, it is highly advantageous to keep the dates stored as Date fields, because comparisons, sorting, and the various date functions will all work as they should.
So if you want to use the date in a different format, you would just use DATE_FORMAT(Release_Date,'%m/%d/%Y') whenever you access it, and leave the field as a native date, as in
SELECT DATE_FORMAT(Release_Date,'%m/%d/%Y') FROM Songs WHERE Release_DATE IS NOT NULL;
It is not possible to "update" the internal format of a MySQL date. If you want to display your text as mm/dd/yyyy, then you should only need a single call to DATE_FORMAT, e.g.
SELECT DATE_FORMAT('2019-07-29', '%m/%d/%y')
which prints:
07/29/19
As Tim suggests, you don't need to change the existing date format & value but if you insists, you could add another column - datatype VARCHAR - in the table and then update the column according to your desired date format. Steps are below:
create new column
ALTER TABLE songs
ADD COLUMN `rls_date` VARCHAR(50) AFTER `release_date`;
Update new column with desired date format
UPDATE songs SET `rls_date`=DATE_FORMAT(`Release_Date`,'%m/%d/%Y');
Just remember, by doing this, you can't expect the column to identify any date format related function outright. Lets say you run a query like this SELECT * FROM songs WHERE rls_date=CURDATE(); won't work.

How to specify the date format inside create table command

I have to create a table with the following columns
Emp_no [PK], Basic_pay_fixed_at, Date_of_birth
The date format for the Date_of_birth column is dd-mm-yyyy
As far as I know the default format for the date datatype in SQL is yyyy-mm-dd
How do I specify this alternate date format inside the create table command ? So, that when I insert value into the table I can insert in the dd-mm-yyyy format.
Thanks in advance!
MySQL (and most other RDBMS as far as I know) use YYYY-MM-DD as the standard string representation and cannot be changed. Any data inserted into such a field must be formatted in this manner, or converted to a true date value using functions such as STR_TO_DATE.
Similarly, selecting a values from a field actually returns datetime types in most client languages, which can then be formatted as needed; or other date functions can be used in the select expressions to yield the desired string.
Dates are stored internal so formatting is for input and output.
This will convert to the style you want for output:
select convert(varchar(10), Date_of_birth, 105) from Table

How to reformat a date values of a MySQL column?

I want to copy data of table into another but I have a pain for a column.
Table A has a column DP of type VARCHAR(10) holding date values in String format this way 'dd/MM/yyyy'.
Table B has a column DC of type DATE holding date values in Date format this way: 'yyyy-MM-dd'.
If I alter the column DP to type DATE, then all its values will be converted to "0000-00-00".
And if I try to copy directly, an error will raise preventing from copying because DP values will not be recognized as DATE values in column DC.
So either, find a way to reformat DP values to DATE values respecting the pattern 'yyyy-MM-dd' and then copy or find some function to convert the french date to english date while copying.
Does anyone have a clue?
Regards!
The STR_TO_DATE function can handle this for you.
STR_TO_DATE(`DP`, "%d/%m/%Y")
You need 3 queries to do that:
ALTER a table to add a new column of DATE datatype
Use UPDATE query with STR_TO_DATE function to move your dates into new column in proper format
ALTER the table to drop the old column and rename new column to what it's supposed to be