String to Date Formatting in BigQuery - sql

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')

Related

Convert "Fri 12/25/20" to date 2020-12-25 in bigquery

I uploaded a google sheet into BQ and the date format is "DOW mm/dd/yyyy" and of course BQ recognizes it as a string. I want to convert it to date without the DOW. How can I do this without changing the date format in google sheets (which is not an option)?
use below
select parse_date('%a %m/%d/%y', 'Fri 12/25/20')
with output
You just need to use the CAST as DATE function with the appropriate format string

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')

SQL Date Formatting from String

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

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

MS Access - Select Char as Date and doing a date diff

I have two columns. ColA and ColB contains char(10) with data "20090520" and "20090521".
I want to select and get the date difference in days. I have tried using Format() and CDate()
but MS Access always display as #ERROR.
Access prefers its dates in this format:
#2009-12-01#
You can convert your date to something Access understands with:
CDate(Format([ColA], "0000-00-00"))
Or alternatively:
DateSerial(Left([ColA],4),Mid([ColA],5,2),Right([ColA],2))
And to display the result in your preferred format:
Format(<date here>, "dd-mm-yyyy")
Try using DateSerial() to convert the dates:
DateSerial(Left([FieldName],4),Mid([FieldName],5,2),Right([FieldName],2))
If at all possible, change the datatype to a date datatype. You should not store dates as character data.
I am connecting to another database which I have no control on. That is why this problem occurred. Thanks for the feedback.