Cast String into Date in BigQuery when Date is in the following format: DD-mon-YYYY - google-bigquery

I have a string that is a date and it is in this format 'dd-mon-yy' ie:
27-Jan-18
28-Jan-18
How do I convert it into date with this format 'dd/mm/yyyy' using Standard SQL in BigQuery?

If you want to convert dates from the string format to a particular date format, you will first have to convert the string to a date using the PARSE_DATE function in BigQuery and then use the FORMAT_DATE function to convert the date into the desired format. Here is a snippet of code that converts the string of type "DD-Mon-YY" to a date value of the format dd/mm/yyyy:
SELECT FORMAT_DATE("%d/%m/%E4Y", PARSE_DATE("%d-%b-%y", "name_of_column_with_string_dates")) as formatted_dates FROM `projectId.dataset.table`

Related

How do I cast a timestamp column as a MM/DD/YYYY in BigQuery

I'm trying to cast the results of an entire column in BigQuery in MM/DD/YYYY format
Time_Column
2022-05-26T17:32:41.000Z
2022-05-28T06:34:23.000Z
Results:
We can try to use FORMAT_DATE function to make it, we can refer to this link Supported Format Elements For DATE to use your expected date format from the datetime type.
SELECT FORMAT_DATE("%m/%d/%Y", Time_Column)
FROM T

Convert yyyymm String to yyyy-mm-dd hh:mm:ss date time in athena

I am trying to convert a string column "yyyymm" to a "yyyy-mm-dd hh:mm:ss" datetime column in athena.
How can I do this?
is this what you want?:
select date_parse(strdatecol,'%b-%d-%Y')
from table
date_parse() works perfectly fine in this case. It reads a string value, the format of the string value and then converts that string value to datetime value. In this case, yyyymm was passed as the string and the format was year and month value.
This gave me the result I was looking for:
SELECT date_parse('yyyymm', '%Y%m')

How to convert string to date in pyspark

I have a file name from which I'm extracting date: some_file_name_20201103114823.csv using substring:
substring(input_file_name(),16,8)
I extracted date part, which is now string: 20201103
How can I convert this string to date in format: MM-dd-yyyy ?
This is what i did, formatted string and then cast it to date - not pretty but does the work:
to_date(concat(substring(input_file_name(),16,4),lit("-"),substring(input_file_name(),20,2),lit("-"),substring(input_file_name(),22,2)),"yyyy-MM-dd")
Reference: https://sparkbyexamples.com/pyspark/pyspark-substring-from-a-column/#:~:text=In%20PySpark%2C%20the%20substring(),using%20substring()%20from%20pyspark.

Format date in SQL for a specified format

I have recieved a particular date in the particular format
2015-06-13T21:49:13.395-07:00 which is inserted as varchar in database.
I want to convert this date into DATE format but am not getting the right formatter for the same
Use to_timestamp_tz() function to convert your string to a value of timestamp with timezone data type:
select to_timestamp_tz('2015-06-13T21:49:13.395-07:00'
, 'yyyy-mm-dd"T"hh24:mi:ss.ffTZH:TZM') as res
from dual
Result:
RES
----------------------------------
13.06.15 21:49:13,395000000 -07:00
SELECT CAST(LEFT(REPLACE('2015-06-13T21:49:13.395-07:00','T',' '),23) AS DATETIME)

how to covert string datatype to date datatype in HIVE?

I have a date in string format in hive table (like "20121021") How do I convert this into "yyyy-mm-dd" (ex: 2012-10-21 or 2012/10/21)?
You can also use cast():
select cast(substr(col, 10) as date)
At least, this works for the YYYY-MM-DD format. I should also note that in a date context, a string such as YYYY-MM-DD will typically be converted automatically.
You can use TO_DATE(). Try following:
TO_DATE('20121021')
Or
from_unixtime(unix_timestamp('20121021', 'yyyyMMdd'),'yyyy-mm-dd')