Convert a value(string) to timestamp (Input string has unwanted characters) - SQL - mysql

I'm trying to convert a value in my table to a timestamp but the value in the table is as below
There's a character 'T' after the date in the string.
GMT_TIME
20210608T111722.837 GMT
1st Goal:
Achieve the result in the below format
Output :
GMT_TIME
2021/06/08 11:17:22:837
2nd Goal: Trying to convert it into EST time
Output
2021/06/08 07:17:22:837
'''
select DATE( DATE_SUB( '1st goal output') , INTERVAL 4 HOUR ) ) from table1
'''
Please give me your valuable input on this.
Thank you

SELECT CAST(SUBSTRING_INDEX(REPLACE('20210608T111722.837 GMT', 'T', ''), ' ', 1) AS DATETIME(3))
Output: 2021-06-08 11:17:22.837.
For to convert from GMT to EST use CONVERT_TZ() function. Remember - you must import timezones into your server system database for to use mnemonic TZ names (not needed if you use time-formatted offsets).
If you want to substract 4 hours unconditionally then
SELECT SUBSTRING_INDEX(REPLACE('20210608T111722.837 GMT', 'T', ''), ' ', 1) - INTERVAL 4 HOUR
In this case the output datatype will be DATETIME(6), if you need strictly DATETIME(3) then use explicit CAST() additionally.

Related

Covert varchar(150) to datetime in sql workbench

I have a column with values '2015-02-14 12:23 AM' declared as varchar(150), I tried using to_date, convert and cast but not able to change the format. I would need this to filter on specific month/year/day. Thanks for the help
PS: Mysql instance is running on RDS through amazon AWS - not sure if its relevant
I suggest storing dates on proper date data type.
to_date is an Oracle function.
In MySQL, you can use STR_TO_DATE to converts a string to date then use Date_format to give the format you need
SELECT DATE_FORMAT(STR_TO_DATE(dt,'%Y-%m-%d %h:%i %p'),'%m/%Y/%d')
from test;
Demo
You can use substring with concat without the need of converting to date format:
select concat(substring(dt,6,2),'/', substring(dt,1,4),'/',substring(dt,9,2)) as my_date
from test;
Demo
SELECT CAST( SUBSTRING_INDEX( '2015-02-14 12:23 AM',
' ',
2
)
AS DATETIME
)
returns the datetime value. If you need to use this value in datetime context then you may remove CAST, it will be performed implicitly:
SELECT TIMESTAMPDIFF ( MINUTE,
SUBSTRING_INDEX( '2015-02-14 12:23 AM',
' ',
2
),
'2015-02-14 12:34'
)

Parse date time with time offset in T-SQL

I have dates in a following form (text):
2016-07-05T13:09:35-06:00
It's ODBC format, so I was able to simply:
SELECT CONVERT(DateTime2, REPLACE(LEFT(TimeModifiedUnparsed, 19), 'T', ' '), 120)
That get's me DateTime2, but without time offset.
I can parse and convert Hour and Minute and Sign out of last 6 characters and then do DATEADD.. but I thought maybe there is a better way to convert such a strings to utc DateTime in SQL Server?
select convert(datetime2, cast('2016-07-05T13:09:35-06:00' as datetimeoffset), 1)
Result:
2016-07-05 19:09:35.0000000

Convert Date to numeric

How can I convert a date like 'Jan 1 2014 12:00AM' to 20140101?
when the month is less then 10 I need to put a zero.
I tryed with datepart but had problem adding '0' before it.
thank's.
SELECT YEAR(DateField) * 10000 + MONTH(DateField) * 100 + DAY(DateField)
To convert it to an integer. Or
SELECT convert(char(8), DateField, 112)
To convert it to a yyyymmdd string.
If your date is currently a string, you need to convert it to a real datetime value first:
SELECT CONVERT(datetime, 'Jan 1 2014 12:00AM')
(mon dd yyyy hh:miAM (or PM) is the SQL Server default format for datetime conversions, so there is no need for a format specifier here.)
If it's already a datetime, you can simply use formatting option 112, which represents YYYYMMDD:
SELECT CONVERT(nvarchar(8), myDateTimeField, 112)
(Nesting the two statements as required or converting the string result to a numeric data type is left as an exercise to the reader.)

How to convert dates in "24-Jun-2013" format to MySQL?

I am trying to convert a large table that has dates in the format Day of Month - Month Abbreviation - Year in a row separated by dashes, like 01-Jan-2014. The closest question I found is this one where the asker ended up doing a search and replace method instead. I could do that too, but I think that surely there is a better way. My code:
SELECT STR_TO_DATE(datecolumn, '%c/%d/%Y') FROM 'Table'
Returns null with dates in the above format but is fine with other date formats. I also tried DATE_FUNCTION and other suggestions I saw online.
This will do it:
SELECT
DATE_FORMAT(
STR_TO_DATE(
REPLACE(
SUBSTRING_INDEX(datecolumn,'-',-2),
'-',
CONCAT(' ', SUBSTRING_INDEX(datecolumn, '-', 1), ', ')
),
'%M %d, %Y'
),
'%Y-%m-%d'
)
FROM `Table`;
Explanation:
We use SUBSTRING_INDEX(datecolumn,'-',-2) to get the last two pieces, like Jan-2014
We replace the - in Jan-2014 with the first piece (using SUBSTRING_INDEX), i.e., 01, but also use CONCAT to add spaces and ,, so we get Jan 01, 2014
We use STR_TO_DATE and tell it the format is %M %d, %Y
We then put it in %Y-%m-%d format with DATE_FORMAT()
Examples:
07-Jan-2014 -> 2014-01-07
9-May-2001 -> 2001-05-09
13-Dec-1978 -> 1978-12-13
Here's a fiddle

Mysql wildcard query problem

WHERE `time` BETWEEN 'Jan 1 %' AND 'Jan 8 %' AND ...
Results
Jan 1 00:33:23
Jan 10 08:52:05
How would I avoid the Jan 10 results?
I have tried a few different combination with %, <=, etc.
Any ideas?
Thanks
WHERE Time >= '1/1/2011' AND Time < '1/9/2011'
Or, if you want results from any year:
WHERE DATEPART( month, Time ) = 1 AND DATEPART( day, Time ) < 9
Currently with this query you confuse the search engine because "between" is designed for use with numbers or dates, while you use it with strings.
There are 2 solutions to your problem:
1) Convert your "time" field to "date" and store the dates as "01-01-2010 00:00" (This is the most healthy solution as you will make the DB aware that is a date field)
2) Try:
WHERE `time` >= 'Jan 1' AND `time` < 'Jan 2'
The second solution may give strange results as it is comparing 2 strings, not 2 dates.
I can't try it for you here, but why don't you try around with the STR_TO_DATE method? You can change your time field to a real date, and then don't need to do that string compare:
WHERE STR_TO_DATE(`time`,'%d,%m,%Y') BETWEEN
'2010-01-01'
AND
'2010-01-09'
also, see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date