I have customers data translated into DB table in which few cell values in column type Varchar, contains date and few doesnt. The ones that contains Date in certain format, need to be converted into another one which will be further used for data analysis jobs. I tried below query but it shows errors for the cells which doesnt have date. How do I resolve this?
Basically I want to strip off the actual time and append 000000 in place of actual time, along with the date.
Query:
UPDATE Table1
SET C1 = DATE_FORMAT(C1,'%Y%m%d000000')
WHERE DATE_FORMAT(C1,'%Y%m%d%H%i%s') AND C1 IS NOT NULL;
Error:
Code : 1292
Truncated incorrect datetime value: 'EVN'
You need to use STR_TO_DATE() to parse the date that's in the VARCHAR column.
You can use a regular expression to test if the column contains a date in MM/DD/YY format.
UPDATE Table1
SET C1 = DATE_FORMAT(STR_TO_DATE('%m/%d/%y', C1), '%Y%m%d000000')
WHERE C1 RLIKE '^[01][0-9]/[0-3][0-9]/[0-9][0-9]$'
Related
I have a table in which there is a column called "DATE" which contains dates in the format "23-Nov-2017" as datatype VARCHAR. I'm trying to convert this VARCHAR column and store it in a new column called "NEWDATE" of datatype DATE.
I have created the new column "NEWDATE" of type DATE and I am trying to use the STR_TO_DATE() function to perform the conversion. However, I can't get it to work for some reason.
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%m-%Y');
The NEWDATE column is not updated with any values after the statement. I guess this means that the statement does not execute. What am I doing wrong?
EDIT: I have also tried STR_TO_DATE(DATE,'%d-%b-%Y'). However there is still no change to the values in the NEWDATE column
Your format '%d-%m-%Y' does not match your actual date string "23-Nov-2017"
The %m is for numeric month and you have an abbreviated text month
Use %b for 3 char month values like this:
STR_TO_DATE(DATE,'%d-%b-%Y')
EDIT: WorkBench issue
That is just a Workbench config setting to stop you accidentally issuing a HUGE update. You can either turn that setting OFF or frig it a bit by giving it a WHERE clause that will allow it to run like below. Below assumes this table has an id column
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%b-%Y') WHERE id<10000000;
Or
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%b-%Y') WHERE id>0;
I am running an append query that selects records from an existing table in the database. All the fields in the receiving table are defined as text. Since this table will receive new records on a regular basis, I want to add a file date in text form to the record when it's written so I can identify when each record was added. I don't need the file date to be an actual date, and it will not be the date I'm actually running the query. The field is defined as text, and the query prompts for the date.
Query from comment:
INSERT INTO tblMaster_Vendor ( Vendor_ID, FileDate )
SELECT DISTINCT FULL_EXTRACT.Vendor_ID, [File Date] AS Expr1
FROM FULL_EXTRACT
WHERE FULL_EXTRACT.Vendor_Type = "84"
Every time, I get a 'data type conversion error', even though I'm adding text (like 20171106) to a text field.
Here's what I've tried:
- changed the field name and prompt from 'File Date' to 'Entered' in case there was a reserved word issue
- Entered straight text into the parameter query box (aaaabbcc)
- Changed the table field type to date/time and entered the date as 11/10/2017
- Entered the text in the parameter box with quotes ('aaaabbcc')
If I don't try to enter the file date as part of the append query, I can run an update query after the append and update the field to my desired value (20171106).
Has anyone seen this behavior before?
Have you tried using the Cstr function? e.g., something of this structure?
INSERT INTO Table2 ( ID, fileupdated ) SELECT Table1.ID, CStr(Now()) AS Expr1 FROM Table1 WHERE (((Table1.ID)>2));
I have 5000+ dates in the following format.
00-00-0000
And the column data type is varchar. If I change the column type then all my rows are put to 00-00-0000 rather than converting the string literal to a date.
Is it possible to change all 50000+ rows to datetime and also the column data type? What would be the best way to do this?
Create a temporary DATE column and update it using STR_TO_DATE function:
UPDATE mytable
SET temp_date = STR_TO_DATE(varchar_date, '%m-%d-%Y')
Then drop the varchar date column and rename the temp date column.
Good Morning All;
I currently have a MySQL table where there are 3 date fields (Columns) that were loaded as strings in this format 20140101 YYYYmmdd. I would like to convert this to a date format 2014/01/01 YYYY/mm/dd. Can someone please provide a simple sql syntax that would alter the table to a date format from a string and change the column to display the dates like this 2014/01/01 and not like 20140101. Thanks to all
Try this:
date_format(str_to_date(datecolumn, '%Y%m%d'),'%Y/%m/%d')
If you just want to reformat the values in the VARCHAR column, assuming that the column with sufficient length e.g. VARCHAR(10), and all the values are eight characters in length...
You could do something like this:
UPDATE mytable t
SET t.mycol = CONCAT( LEFT( t.mycol ,4)
, '/'
, SUBSTR( t.mycol ,5,2)
,'/'
, SUBSTR( t.mycol ,7,2)
)
WHERE CHAR_LENGTH(t.mycol) = 8
We want something in the statement that will prevent the statement from "working" a second time, if it's inadvertently re-run. It doesn't have to be CHAR_LENGTH. We might want to include a check that the value doesn't already contain a slash character AND t.mycol NOT LIKE '%/%'.
But why on earth are "date" values being stored in character columns, rather than in DATE datatype, which is custom designed for storing and working with date values?
ALTER TABLE mytable MODIFY mycol DATE ... ;
(If the column is defined as NOT NULL, has a default value, has a comment, those attributes can be retained, they need to be included in the new column specification, e.g.
ALTER TABLE mytable MODIFY mycol DATE NOT NULL COMMENT 'creation date';
Note that DATE columns do not have a "format" per se. When converting to string, MySQL uses date format '%Y-%m-%d'. And MySQL expects string literals representing date values to be in that same format. To get a value from a DATE column converted to string in format 'yyyy/mm/dd'.
SELECT DATE_FORMAT(date_col,'%Y/%m/%d') AS date_col
To get a string value in that format converted to DATE datatype
SELECT STR_TO_DATE('2015/06/01','%Y/%m/%d')
I have a table that contains two columns that I'd like to change their data type. Both are currently just declared as text but I'd like to change DateSent to a date and TimeSent to a time. The data is currently in MM/DD/YYYY format for date and HH:MM (24h) for time.
How can this be done? And when I add data in the future, will it automatically convert any reasonable dates and times format correctly?
You can't do this in one step. What you will need to do is to add a new column for your date, and then populate it using an UPDATE statement that converts the string to a date; using CAST('mm/dd/yyyy' AS DATE). Then you can delete your old column, and rename the new one.
The same steps for the column holding the time data.
You will need to add new data in the future in the correct types; date or time.
Per OP request I'll edit Mark's answer with a sample query
SAMPLE UPDATE QUERY:
UPDATE table_name t,
(
SELECT
id,
CAST(col1 AS DATE) as update_col1
CAST(col2 AS TIME) as update_col2
FROM table_name
) as t1
SET t.col1 = t1.update_col1, t.col2 = t1.update_col2
WHERE t.id = t1.id