Convert varchar string data to time format in mysql - mysql

I need to convert varchar string data to time format in mysql.
For example,
I have a varchar column in table which stores time. The accepted values should be like 9:30 AM or 1200 PM. But currently it has either blank values or it has values like 9.30am or 12:00
There are many records like this, so cannot update manually.
Ithere any work around or function or procedure to do so?
please help.
Thanks

You can use the STR_TO_DATE() MySQL function to convert any string to a date.
Additionally you can use TIME() to extract the time portion of a datetime. A combination of both function is used to convert an arbitrary date string to a datetime and then you can extract the time portion from it as a valid MySQL TIME.
By default MySQL functions follow standard format but custom format can be specified and if your values don't use the international formats you'll need to check with the documentation and provide the format your system is using.

Related

Parsing Custom Date Format in MySQL

Problem
I need to query out a date value for use in some ETL processing. However, the data is stored in MySQL as a VARCHAR column in values like 1/1/19.
What I've tried
A simple CAST(myDateColumn as DATE) fails as I get values like 0001-01-19 returned back.
Question
Am I able to pass a custom date format string into the CAST call somehow to tell it how to parse out the date parts? If not, could a SUBSTRING type of function help here?
As discussed in the comments above, you can use the STR_TO_DATE() function to parse a string into a proper YYYY-MM-DD date.

MySQL timestamp format and datediff

Hi I'm writing queries for MySQL, and now my database has a column containing the timestamp in this format: 7/14/2015 7:57:49 AM, but I need to use the DATEDIFF function, so how can I convert the timestamp into the format like: 2015-7-14 (or 2015-07-14, I'm not sure which one is correct; just the date)?
This should convert your string to just the date in a date format, then you can use DATEDIFF on the date fields in question:
SELECT STR_TO_DATE(LEFT(t,LOCATE(' ',t) - 1), '%m/%d/%Y') FROM my_table;
The LEFT function will take the substring to the left of the space, which is just your date, then STR_TO_DATE will convert that substring to a date the system can use.
(Not knowing your field and table names, I used t and my_table.)
You don't need to. The way MySQL displays timestamps has nothing to do with the way they're stored internally; as long as it's TYPE TIMESTAMP or some compatible type, the DATEDIFF() function will know what to do with it.
TIMESTAMPs are actually stored as a really huge integer representing (I think) milliseconds from Midnight UTC, January 1st, 1970. The display format is determined by a system global variable, and has nothing to do with the actual value.
Converting from a string to a DATETIME or TIMESTAMP is actually also fairly straightforward using the STR_TO_DATE() function; in your case the format string would be something like
STR_TO_DATE('%c/%e/%Y %l:%i:%s %p', datecol)
although you might have to experiment a bit to make it work reliably.

Wrong MYSQL (date) output

i have a database that contains over 2000 records and the date is wrongly formatted (mm/dd/yyy). I need to change this with mysql into dd/mm/yyy.
i have this code:
UPDATE wp_team_workshop_availbility SET available_date = DATE_FORMAT('available_date', '%d-%c-%y')
but all i creates is an empty field.
If you are storing it as a date (datatype), it is just a date - there is no format. However, you also don't want available_date as a quoted string, which is trying to convert the string "available_date" into a date.
My guess is you have the date stored as a string (you really shouldn't). However, what you will want is something more like:
UPDATE wp_team_workshop_availbility
SET available_date = DATE_FORMAT(STR_TO_DATE(available_date,'%c/%d/%Y'), '%d-%c-%y');
i.e. you need to convert the string to a date and then convert it back to a string.
But really, you should take advantage of this opportunity to change your storage so you are using the right datatype.

How to import Excel's serialized datetime into MySQL

How do I import and preserve date format fields such as "8/21/2012" from Excel to MySQL?
I am using MySQL Workbench and the Excel MySQL Excel data transfer plug in. When I select the Excel data I want to import into MySQL, I get a window where I declare variable types for all fields. All fields and declarations work as expected except for date and time fields. Both date and time switch from 8/21/2012 to a number like 398475 etc. How can I import these fields into MySQL by preserving the dashed mm/dd/yyyy format? I assume the same procedure will work for time as well.
Alternatively, is there a way to convert the serialized datetime value (a float, representing the number of days since 1/1/1900) back to mm/dd/yyyy in MySQL ?
Thank you!
You can convert your date cells into a MySQL supported format using the TEXT function.
=TEXT(A1,"YYYY-MM-DD")
This will convert a date in cell A1 to the yyyy-mm-dd format that the MySQL date field expects.
To get the serialized date format (say 36422) into a useful date format in MYSQL use the interval function added to the base date that Excel uses (which is actually 1900-00-00 but since that doesn't exist you will have to use 1900-01-01 which is why we subtract 2 from the date column)
`'1900-01-01' + INTERVAL(Your_Date_Column - 2)DAY`
You can convert your datetime cells into a MySQL supported format using the TEXT function
=TEXT(A1,"YYYY-MM-DD HH:MM:SS")
Simply use Date (java.util package) for this.
Date d=Date date=cell.getDateCellValue();
Now Use it as you want.

UNIX_TIMESTAMP outputting NULL in MySQL?

I've got a table setup which has populated data. In column "date", I have dates in the following format:
yyyymmdd i.e. 20131110
I have created a new field and called it newdate with the text format.
Then, I open up the SQL window and put the following in
UPDATE wl_daily
SET
newdate = UNIX_TIMESTAMP(date)
For some reason, it is running correctly, however it only outputs NULL to all the rows. Also, the column name is blank for some reason
Any suggestions?
That's because your field in a string and you're trying to add timestamp to it which is not a string. You need to use a valid datetime field like timestamp for this to work.
Advice: don't store dates and times as strings. Store them in their native format. It makes working with dates and times much easier.
While John Cronde's answer is correct - it doesnt help your situtation
UNIX_TIMESTAMP(STR_TO_DATE(`date`, '%Y%m%d'))
will do the conversion for example
SELECT UNIX_TIMESTAMP(STR_TO_DATE('20131111', '%Y%m%d'))
returns
unix_timestamp(STR_TO_DATE('20131111', '%Y%m%d'))
---------------------------------------------------
1384128000
You should only use this to convert your columns to the date specific columns. Converting each time you need a number will add load and slow down the query if used in production