Converting STRING to DATE in BigQuery - string

I have a column called order_date in a table in BigQuery and it is of the data type STRING. It has two types of values 10/2/2020 and 2020-10-02. PARSE_DATE('%m/%d/%Y', order_date) converts 10/2/2020 to DATE and CAST(order_date as DATE) converts 2020-10-02 to DATE. I wrote the CASE statement below to handle both of the cases but it is erroring out.
CASE
WHEN FORMAT(order_date) ='%m/%d/%Y'THEN PARSE_DATE('%m/%d/%Y',order_date)
ELSE CAST (order_date AS DATE) END AS order_date

You could use a regex pattern to assert the %m/%d/%Y variant:
SELECT
CASE WHEN REGEXP_CONTAINS(order_date, r"^\d{2}/\d{2}/\d{4}$")
THEN PARSE_DATE('%m/%d/%Y', order_date)
ELSE CAST(order_date AS DATE) END AS order_date
FROM yourTable;

Related

How to extract the year and quarter from the String date in databricks SQL

Can someone show me how to extract the year from the String date in databricks SQL.
I am based in the UK and our date format is normally as follows:
dd/mm/yyyy
The field containing the dates is set as StringType()
I am trying to extract the year from the string as follows:
select year(cast(financials_0_accountsDate as Date)) from `financiallimited_csv`
I'm using the following the code to extract the quarter
select quarter(cast(financials_0_accountsDate as Date)) from `financiallimited_csv`
However, both result in NULL values.
Any thoughts on how to extract the year and quarter from dates with StringType() dd/mm/yyyy?
The table looks like the following:
Could you try the to_date function?
select year(to_date(financials_0_accountsDate, 'dd/MM/yyyy')) from `financiallimited_csv`

spark dataframe: date formatting not working

I have a csv file in which a date column has values like 01080600, basically MM-dd-HH-mm.
I want to add a column in dataframe which shows this in a more readable format.
I do :
spark.sql("SELECT date...")
.withColumn("readable date", to_date(col("date"), "MM:dd HH:mm"))
.show(10)
But readable date is returned null.
What am I missing here?
While formating or converting to date or timestamp you need to provide the date_format as is following your pattern , example in your case you need to modify your format as below and further which can be formatted depending on the final format you wish your date col to take using date_format
References to various patterns and parsing can be found here
To Timestamp
sql.sql("""
SELECT
TO_TIMESTAMP('01080600','ddMMhhmm') as date,
DATE_FORMAT(TO_TIMESTAMP('01080600','ddMMhhmm'),'MM/dd hh:mm') as formated_date
""").show()
+-------------------+-------------+
| date|formated_date|
+-------------------+-------------+
|1970-08-01 06:00:00| 08/01 06:00|
+-------------------+-------------+

Parsing non iso datetime string to just date part in presto

I have table which stores datetime as varchar
Format looks like this 2018-07-16 15:00:00.0 ,
I want to parse this to extract only date part so that I use date part to compare with date in string format such as '2018-07-20' in where clause. What is the best way to achieve this in presto?
This particular format (based on example value 2018-07-16 15:00:00.0 in the question) is understood by cast from varchar to timestamp. You then need to extract date part with another cast:
presto> SELECT CAST(CAST('2018-07-16 15:00:00.0' AS timestamp) AS date);
_col0
------------
2018-07-16
(1 row)

Converting values in Athena Presto

I have a few things I want to accomplish with Presto. I currently getting some data in the following formats
date 16-Jan-2018
num 1000
I want to write a query that can convert these values to
2018-01-16
1,000
For the date you could do the following:
select date_parse('date 16-Jan-2018','date %d-%b-%Y')
For the second field, you would have to split it first with split(string, delimiter), then cast the second array element to INTEGER.
Here is the full answer:
SELECT date_parse(date_string,'date %d-%b-%Y') as parsed_date,
CAST(
split(int_string, ' ')[2] AS INTEGER
) as parsed_int
FROM (VALUES ('date 16-Jan-2018', 'int 1000'))
AS t(date_string, int_string)

How to compare just the date, not the timestamp using LINQ

I'm trying to compare just the date of a column but because the query below is also comparing the time I get no result set back.
How could I write this same LINQ query if I'm only interested in the actual date value matching?
The column "ImportDate" has a value that looks similar to this 2009-08-30 12:26:00
from t in UnitsOfWork _
where t.ImportDate = "08/30/2009" _
select t
You can compare it as a string or you can use just the Date property on ImportDate
where t.ImportDate.ToString("yyyyMMdd") == "20090830"
or
where t.ImportDate.Date == new DateTime(2009,8,30)

Resources