issue with mysql STR_TO_DATE() function ending characters - mysql

I am trying to use the str_to_Date function to convert a string like
'30/09/2010 3:33:51 p.m.' and '30/09/2010 12:00:00 a.m.'(excluding quotations)
to convert into a date time.
However, I am having trouble adding the last characters being 'p.m.' or 'a.m.' to function
What I have tried is
SET datecolumn = STR_TO_DATE(left(datecolumn(23), '%d/%m%Y %h:%i:%s %_.m.')
<---- returns error code
and
SET datecolumn = STR_TO_DATE(left(datecolumn(18), '%d/%m%Y %h:%i:%s')
<-- But this one excludes the
'a.m.' or 'p.m.'
TIA

For manageAM/PM Use p%
SET datecolumn = STR_TO_DATE(left(datecolumn(23), '%d/%m%Y %h:%i:%s %p')
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format

Use REPLACE() to remove the dots from am/pm and apply the correct format:
set datecolumn = str_to_date(
replace(left(datecolumn, 24), '.', ''),
'%d/%m/%Y %r'
)
I use the function LEFT() because it exists in your code, although I don't know if it is really needed.
See the demo.
Results:
| date |
| ------------------- |
| 2010-09-30 15:33:51 |
| 2010-09-30 00:00:00 |

Related

How to replace all the digits before hyphen with a new digit using MySQL? [duplicate]

I have a table called myTable which has a column called col1. This column contains data in this format: (1 or 2 digits)(hyphen)(8 digits).
I want to replace all the data in this column and replace everything before hyphen with 4, so this is an example:
--------------------------------
| old values | New Values |
--------------------------------
| 1-654283568 => 4-654283568 |
| 2-467862833 => 4-467862833 |
| 8-478934293 => 4-478934293 |
| 12-573789475 => 4-573789475 |
| 16-574738575 => 4-574738575 |
--------------------------------
I am using MySQL 5.7.19, I believe REGEXP_REPLACE is available in MySQL Version 8+... not sure how this can be achieved?
You don't need regex; you can use SUBSTRING_INDEX to extract everything after the hyphen and concatenate 4- to that:
UPDATE myTable
SET col1 = CONCAT('4-', SUBSTRING_INDEX(col1, '-', -1))
Demo on dbfiddle
This will work regardless of the number of characters after the hyphen.
Looking to your pattern seem you could avoid regexp
update myTable
set col1 = concat('4-', right(col1,8))
or
update myTable
set col1 = concat('4', right(col1,9))
Try this:
UPDATE testing SET val=REPLACE(val,SUBSTRING(val,1,LOCATE('-',val)),'4-');
Fiddle here :https://www.db-fiddle.com/f/4mU5ctLh8NB9iKSKZF9Ue2/2
Using LOCATE to find '-' position then use SUBSTRING to get only the front part of the '-'.
SELECT CONCAT( #new_prefix, SUBSTRING(old_value FROM LOCATE('-', old_value)) ) AS new_value
UPDATE sourcetable
SET fieldname = CONCAT( '4', SUBSTRING(fieldname FROM LOCATE('-', fieldname)) )
WHERE LOCATE('-', fieldname)
/* AND another conditions */

How to replace a regex pattern in MySQL

I have a table called myTable which has a column called col1. This column contains data in this format: (1 or 2 digits)(hyphen)(8 digits).
I want to replace all the data in this column and replace everything before hyphen with 4, so this is an example:
--------------------------------
| old values | New Values |
--------------------------------
| 1-654283568 => 4-654283568 |
| 2-467862833 => 4-467862833 |
| 8-478934293 => 4-478934293 |
| 12-573789475 => 4-573789475 |
| 16-574738575 => 4-574738575 |
--------------------------------
I am using MySQL 5.7.19, I believe REGEXP_REPLACE is available in MySQL Version 8+... not sure how this can be achieved?
You don't need regex; you can use SUBSTRING_INDEX to extract everything after the hyphen and concatenate 4- to that:
UPDATE myTable
SET col1 = CONCAT('4-', SUBSTRING_INDEX(col1, '-', -1))
Demo on dbfiddle
This will work regardless of the number of characters after the hyphen.
Looking to your pattern seem you could avoid regexp
update myTable
set col1 = concat('4-', right(col1,8))
or
update myTable
set col1 = concat('4', right(col1,9))
Try this:
UPDATE testing SET val=REPLACE(val,SUBSTRING(val,1,LOCATE('-',val)),'4-');
Fiddle here :https://www.db-fiddle.com/f/4mU5ctLh8NB9iKSKZF9Ue2/2
Using LOCATE to find '-' position then use SUBSTRING to get only the front part of the '-'.
SELECT CONCAT( #new_prefix, SUBSTRING(old_value FROM LOCATE('-', old_value)) ) AS new_value
UPDATE sourcetable
SET fieldname = CONCAT( '4', SUBSTRING(fieldname FROM LOCATE('-', fieldname)) )
WHERE LOCATE('-', fieldname)
/* AND another conditions */

Convert date column in MYSQL to different format

I need to update column pubDate_Date to 2018-02-04 20:39:55 from column pubDate with format Sun, 04 Feb 2018 20:39:55 +0100.
I have tried the following, but without success:
UPDATE `feeds` SET `pubDate_Date` = date('Y-m-d H:i:s', strtotime(`pubDate`))
Hope someone can point me in the right direction
In general, you can use MySQL's STR_TO_DATE() function:
UPDATE feeds SET pubDate_Date = STR_TO_DATE(pubDate, '%a, %d %b %Y %T')
However there's no format code for timezone offsets, which MySQL will therefore ignore from the end of your original string (instead interpreting each string to be in the session's time_zone). Therefore:
If all records are in the same timezone, you could simply do SET time_zone = '+01:00' before the above update command.
Otherwise, do the following instead to adjust each time to the intended timezone:
UPDATE feeds SET pubDate_Date = CONVERT_TZ(
STR_TO_DATE(pubDate, '%a, %d %b %Y %T'),
##session.time_zone,
CONCAT(SUBSTR(pubDate, -5, 3), ':', RIGHT(pubDate, 2)
)

MySQL STR_TO_DATE() function returns null

I wanted to convert my date format From MMMM dd,yyyy to yyyy-MM-dd.
I tried using the following:
SET #dt_to = STR_TO_DATE(dateTo, '%d-%m-%Y');
but returns a NULL value.
How will I convert my date to yyyy-MM-dd format in MySQL?
EDITED:
I am creating a procedure in which the value of dateTo was received in the parameter. It is a date in MMMM dd, yyyy format. E.g. October 10, 2015.
NOTE:
The whole query does not return NULL when I use:
SET #dt_to = dateTo;
To convert the date format first you need to use STR_TO_DATE to convert the input string to a date value
SET #dt_to = STR_TO_DATE(dateTo, '%M %d,%Y');
and then convert that date value to your required format
SET #dt_converted = DATE_FORMAT(dt_to, '%Y-%m-%d');
or all in 1 go
SET #dt_to = DATE_FORMAT(STR_TO_DATE(dateTo, '%M %d,%Y'), '%Y-%m-%d');
If it's returning null then that means the extracted datetime value is illegal. You can try like below. See MySQL Documentation for more information.
SELECT STR_TO_DATE('October 10, 2015','%M %d,%Y');

MySQL inverse substr()

select now() will output 2012-11-20 09:05:38.
select substr(now(), locate(' ', now())) will output 09:05:38.
How can I obtain this result: 2012-11-20 instead? Been there, done that but can't achieve it yet. Forgive me for my lacking but please help me. Thanks!
Not an specific answer to your question about substrings, but if I wanted the DATE part of NOW() I would do:
SELECT DATE(NOW());
Or for the time portion
SELECT TIME(NOW());
For actual string purposes though you can use:
SUBSTR('SOME STRING', 0, LOCATE(' ', 'SOME STRING')) <- returns from position zero for a length of 4 characters
Or
SUBSTRING_INDEX('SOME STRING', ' ', 1) <- gives 'SOME'
SUBSTRING_INDEX('SOME STRING', ' ', -1) <- gives 'STRING'
you can use DATE_FORMAT
SELECT DATE_FORMAT('2012-11-20 09:05:38', '%Y-%m-%d')
or just DATE
SELECT DATE('2012-11-20 09:05:38')
SQLFiddle Demo (both queries)