SQL Date Formatting from String - sql

I am stuck trying to convert the following strings formatted like this 26-09-2021-02-54-03 (DD-MM-YYYY-hh-mm-ss) into timestamp or in this format YYYY-MM-DD HH:DD:SS in BigQuery. Any idea how to process?
I cannot use PARSE_TIMESTAMP() since there is no T in the string.
Thank you

You should be able to parse it with the following:
select '26-09-2021-02-54-03'
, PARSE_TIMESTAMP("%d-%m-%Y-%H-%M-%S", '26-09-2021-02-54-03')
, FORMAT_TIMESTAMP("%F %X",PARSE_TIMESTAMP("%d-%m-%Y-%H-%M-%S", '26-09-2021-02-54-03'))
For more information on the format elements see:
https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#supported_format_elements_for_timestamp

Related

MariaDB converting datetime string to date

Trying CONVERT()
Trying STR_TO_DATE()
Trying CAST() - neither DATE or DATETIME work
Trying CAST(STR_TO_DATE())
Hi, I cannot figure out what I'm doing wrong with trying to convert my varchar column Procedure_Date containing a string in the dd/mm/YYYY hh:mm:ss format. I am trying to extract just the date from this string but I seem to be cursed. Using MariaDB and have gone through all the docs there. Greatly appreciate any advice!
You can use cast
SELECT Procedure_Date, CAST(STR_TO_DATE(Procedure_Date, '%d-%m-%Y') AS DATE) FROM uniTable5;
Use: DATE('YOUR DATETIME')
EXAMPLE: DATE('2022-04-25 14:50:01')
OUTPUT: 2022-04-25. Result in DATE.

How to convert DD-MMM-YY to YYYY/MM/DD in Big Query SQL

I've one date format as DD-MMM-YY in one of my Big Query table as a STRING column (e.g 31-OCT-20).
Now I need to convert the format to YYYY/MM/DD in Date format to insert data to another table (e.g 2020/10/31).
Please help with the required format for Google Big Query.
Thanks in advance.
It worked after trying a combination of PARSE_DATE and FORMAT_DATE:
SELECT FORMAT_DATE("%Y/%m/%d",PARSE_DATE('%d-%b-%y','31-OCT-20'))
Use PARSE_DATE:
SELECT PARSE_DATE('%d-%b-%y','31-OCT-20')

String to Date Formatting in BigQuery

I have a list of strings such as 6/20/2019 that I need to convert to date format in BigQuery. Have issues with the leading 0's and this text being recognized as an actual date/time format when being used in BigQuery SQL or being pulled into Tableau as such.
Doesn't parse_date() work?
select parse_date('%m/%d/%Y', '6/20/2019')

Convert YYYYMMDD to MM/DD/YYYY in Snowflake

I need help in figuring out the date conversion logic in Snowflake. The documentation isn't clear enough on this.
In SQL Server, I would try
SELECT CONVERT(DATE, '20200730', 101)
and it gives me '07/30/2020'.
If I try the following in Snowflake,
to_varchar('20200730'::date, 'mm/dd/yyyy')
it gives me '08/22/1970'. Why would it give an entire different date? Need help in getting the logic with the correct date.
The issue with what you are doing is that you are assuming that Snowflake is converting your string of '20200730'::DATE to 2020-07-03. It's not. You need to specify your input format of a date. So, 2 options based on your question being a bit vague:
If you have a string in a table and you wish to transform that into a date and then present it back as a formatted string:
SELECT TO_VARCHAR(TO_DATE('20200730','YYYYMMDD'),'MM/DD/YYYY');
--07/30/2020
If the field in the table is already a date, then you just need to apply the TO_VARCHAR() piece directly against that field.
Unlike SQL Server, Snowflake stores date fields in the same format regardless of what you provide it. You need to use the TO_VARCHAR in order to format that date in a different way...or ALTER SESSION SET DATE_OUTPUT_FORMAT will also work.
Try select to_varchar(TO_DATE( '20200730', 'YYYYMMDD' ), 'MM/DD/YYYY'); which produces 2020-07-30
You may need to refer to https://docs.snowflake.com/en/user-guide/date-time-input-output.html#timestamp-formats

Converting Varchar(200) to YYYY-MM-DD format in Teradata SQL

I have a Varchar like so:
23FEB2025
I am trying to convert it into a format like:
1994-02-23 or YYYY-MM-DD
I have tried select cast ('23FEB2025' as date format 'yyyy-mm-dd'); and sel convert(date,'23FEB2025')
There are other dates in the column that are formatted like 12DEC65.
I am now starting to assume that there is no simple way to convert this so I am asking for a little guidance. Would i need to take sub strings of the date and use a bunch of select case statements?
I was hoping to find a short way to do this but it seems there might not be one. I read on here that storing dates as a string is a bad idea and I fully subscribe to that notion now.
Thank you for any help or advice!
The format portion of casting a date is the input format. The output format is based on your locale and date settings. In your case, you want this:
select
cast ('23FEB2025' as date format 'ddMMMYYYY')
Which will return 2025-02-23.