Converting string column to timestamp in BigQuery? - mysql

I am trying to convert a column in a BQ table to timestamp. I have two string columns, one for utc_hour (string 0-23) and utc_day (string yyyymmdd) imported from a public data source. I merged the two columns to produce a string column, utc_timestamp, with strings like this - "20171208 500" .
I need to convert that string into timestamp, and when I use
TIMESTAMP(utc_timestamp)
I get the error message
Invalid timestamp: '20171208 500'
I tried using dataprep, which also could not convert that string to a timestamp.
How can I convert this format to a timestamp?

Try to parse with %Y%m%d%k%M format.
PARSE_TIMESTAMP("%Y%m%d%k%M", utc_timestamp)

This question is already answered but in case someone else visits here with his/her own unique timestamp format (in which case the accepted answer might not work), you need to follow the notations on this documentation page
For eg, in my case values were like this 2020-05-11-00:00:00. So, I went to the above-mentioned page and found that the format string that I needed would be something like %Y-%m-%d-%H:%M:%S.
I will put the description for the ones that I used in my format string:
%d The day of the month as a decimal number (01-31).
%H The hour (24-hour clock) as a decimal number (00-23).
%M The minute as a decimal number (00-59).
%m The month as a decimal number (01-12).
%S The second as a decimal number (00-60).
%Y The year with century as a decimal number.
The :s and the -s are quite self-explanatory.

Related

MySQL equivalent of php's strtotime()

Is there a way to convert a string such as "-1 week" or "-5 minutes" into a datetime value in MySQL similar to php's extremely convenient strtotime() function?
I have a table that stores a human-readable time interval (such as "2 minutes") in one column and a datetime in another column.
I would like to select the rows where more than the amount of time specified in interval has elapsed since datetime.
MySQL doesn't have an equivalent of PHP's strtotime() in the sense that there is nothing that will automatically attempt to parse and determine the format of a date string using by assuming multiple formats.
What it does have is STR_TO_DATE(str,format) which requires you specify the format of your date, time or date + time string. It is the equivalent of PHP's date_create_from_format(format, str) function (though the format of the format parameter are different).
Here are some examples given from the MySQL documentation. They show a date being passed along with the format string that lets it know how the date string is to be interpreted:
SELECT STR_TO_DATE('01,5,2013','%d,%m,%Y');
SELECT STR_TO_DATE('May 1, 2013','%M %d,%Y');
Alternatively, you can cast a string to a date, time or datetime type, but they require a specific format (YYYY-MM-DD hh:mm:ss.fraction) for it to work:
SELECT CAST("2019-11-21" AS DATE);
If you deviate too far from that format it will make a few assumptions but could produce an incorrect date.

Extract and convert Datetime field from JSON in SNOWFALKE

I am receiving a Datetime field in my JSON which looks like the below screenshot
where first 4 characters are year, next two characters are month, next two characters are date, next two characters are hour, next two characters are minutes and last two characters are seconds.
I extracted the date successfully and tried to convert using to_timestamp function in SNOWFLAKE but returns me the result like
"2606-08-31 03:17:04.416"
code that I am working with to convert into Timestamp
select distinct to_timestamp(a.value) within group(order by to_timestamp(a.value)) from orders a
Could someone please help me with this?
The default format for conversion is AUTO, which will attempt to guess. It appears as though it is guessing incorrectly.
To specify a format, add a second parameter to your call to to_timestamp that is a string of the format. Here are the docs.
to_timestamp(a.value, 'YYYYMMDDHH24MISS')

MySQL string to date brings all rows with the same year

I have a table that the data is a text in the format: 05/07/2019 (dd/mm/yyyy).
When I try to convert it to a date with
str_to_date(date_field,'%d/%m/%y')
I get the response in the format 2020-06-21 (yyyy-mm-dd). But the problem is that in the result all of the years are 2020 though this isn't the actual sitatuion.
Is there a way to solve this or do I need to recreate the DB with the date to be in date format?
If you are using the format dd/mm/yyyy you need to use capital Y
%Y Year as a numeric, 4-digit value
%y Year as a numeric, 2-digit value
So, in your case, you should use str_to_date(date_field,'%d/%m/%Y') because when you use y, it is taking only the first two digits for the year.
And, as I said in my comment (and Tim also said) if you are storing dates in a RDBMS, you should use ALWAYS proper date/datetime data types.

Converting MySQL dates (from what I think are seconds)

I am pretty new to MySQL, and am looking at a table (through a query) that has three date fields. However, they appear to be in seconds (but I could be wrong), but ultimately, I need to convert them to a valid date/time.
The numbers are:
1366272682
1366239600
1366272682
I think one of these dates is 18th April 2013.
Can someone let me know how I can convert them within the query (or indeed if I am right).
Thank you.
Those "numbers" are actually Unix Timestamps. Use FROM_UNIXTIME() to convert them into human friendly formats:
Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context.
For example:
SELECT FROM_UNIXTIME(1366272682, '%e%D %M %Y')

Convert decimal stored as varchar to datetime

I've inherited a MySQL database with a date column type of Varchar.
The column contains decimal strings. For example: 41143.646585648.
How would I go about converting this into DateTime format?
All you need to know is the date the original developer defined as the value zero. Then it's a simple matter of adding the integer number of days to that date, then multiplying the fractional part by 86400 to get seconds and doing the arithmetic to determine the hour, minute and second.
If you have a row where you know the actual date and the stored value you can determine the zero date easily by subtraction.
You could string replace the dot, but the remaining count of numbers is to high for a unix timestamp. I don't know how the date was converted to decimal. Maybe you should split your string rather than string replace.
Maybe this code gives you some ideas for a solution:
#SELECT FROM_UNIXTIME(REPLACE('41143.646585648', '.', '')); # Does not work due to wrong amount of numbers in string
SELECT FROM_UNIXTIME(REPLACE('13577.77916', '.', '')); # works