Impala joining and converting string columns to create date - impala

I have a data set which has three columns in string format.
Year: YYYY
Date: DD
Month: A string from 1 to 12.
I need help joining and converting this to a single date as YYYY-MM-DD. Appreciate any help / guidance.

You can use the CONCAT_WS function as shown below and then cast the string to timestamp if you so desire:
SELECT
CAST(CONCAT_WS("-", year_column, month_column, day_column) AS timestamp) AS full_date FROM a_database.a_table

Related

How to search in a string for the date between dateFrom and dateTo

I have three parameters, $motivazione, $dateFrom and $dateTo, in my table I have the following string: {"motivation": 17, "dfp": "31/10/2022"}
The query must return the id of the row if the string in the db contains motivazione: $motivazione and dfp: between dateFrom and dateTo,
with the following query I can only get the date contained in the string:
SELECT column, SUBSTRING (column, CHARINDEX ('dfp', column) +6, 10) FROM myTable
but I can't compare it
how can I do?
Thank you
Convert your date string to a SQL Server date: CONVERT(DATE,'31/10/2022', 103).
The format 103 is dd/mm/yyyy. If you don't specify it, the date might be interpreted in the US format mm/dd/yyyy.
Also, if you have a JSON string, use #AnthonyG's advice to extract the date value.
A where clause would be something like this
WHERE CONVERT(JSON_VALUE(Data, '$.dfp'), 103) BETWEEN '2022-01-01' AND '2022-01-31'
Use the year first format for date literals, then you will not have the dd/mm - mm/dd issue.

How to convert 6 digit decimal to date format (SQL)

Would like to convert 6 digit decimal e.g. 310322 (ddmmyy) to Date Format to 2022-03-31?
Thank you.
Please specify what language you are using, I will share in python.
You just have to separate the elements by positions and convert in the format that you want to use, in this case as you can see i added the timezone UTC for better format:
Decimal_value = 310322
Year = int(decimal_value[4:5])
month = int(decimal_value[2:3])
day = int(decimal_value[0:1])
Date_format = datetime(day, month, year).replace(tzinfo=timezone.utc)
In Impala you have to convert it into a timestamp and then do to_date
select to_date(to_timestamp('310322', 'DDMMYY'),'YYYY-MM-DD');

How to convert string YYYYMM to date in hive sql

I have column as string in the format "yyyymm" in hive table, and i would like to convert this to date with day as first of each month. for example 202105 string i would like this to be as date "2021-05-01".
Appreciate your help in this matter.
SELECT TO_DATE(CONCAT(SUBSTR('YYYYMM',1,4),'-',SUBSTR('YYYYMM',5,2),'-01'))
You can try using the following, replace "YYYYMM" with your date value string.

Convert string of YYYYMMDD to YYYY-MM-DD Date format in Snowflake

Based on the example mentioned here in the Snowflake documentation, why are the date and timestamp values returning different values just by changing the ORDER BY clause? Also, I am trying to convert a string to a date format which is not returning correct results in Snowflake while this works fine in other SQL based Engines. Need help from experts on this.
This query
SELECT '20200710', TO_DATE('20200710');
is returning the following output
20200710 | 1970-08-22
Also tried:
SELECT TO_DATE('20200710', 'YYYY-MM-DD');
and got the error:
Can't parse '20200710' as date with format 'YYYY-MM-DD'
To convert to a date data type, you would use:
SELECT TO_DATE('20200710', 'YYYYMMDD')
I would recommend just keeping the date data type. But if you want a string in the format YYYY-MM-DD:
SELECT TO_CHAR(TO_DATE('20200710', 'YYYYMMDD'), 'YYYY-MM-DD')

Presto/SQL - Converting string timestamp to date throws error

NOTE: I am running my query in Qubole's presto and sql command engine.
I am trying to convert my string timestamp to just date but none of the options are working out.
My string timestamp looks like 2017-03-29 10:32:28.0
and I want to have it like 2017-03-29
I have tried following queries to convert this string timestamp to retrieve date
1. select cast(created as date) from table1
Value cannot be cast to date: 2017-05-26 17:23:58.0
2. select cast(from_iso8601_timestamp(created) as date) from table1
Invalid format: "2014-12-19 06:06:36.0" is malformed at " 06:06:36.0"
3. select date(created) from table1
Value cannot be cast to date: 2012-10-24 13:50:00.0
How I can convert this timestamp to date in presto/sql?
As far as explained in the documentation, prestoDB seems to expect timestamps in a format '2001-08-22 03:04:05.321', and dates in a '2001-08-22'.
One solution would be to use a string function to extract the relevant part of the string before converting it. We know that the date part is located before the first space in the string, so.
If you need the date part as a string datatype:
split_part(created, ' ', 1)
If you need the date part as a date datatype:
cast(split_part(created, ' ', 1) as date)
You can try to use one of the following solutions:
SELECT
'2017-03-29 10:32:28.0' AS input_string,
DATE(date_parse('2017-03-29 10:32:28.0', '%Y-%m%-%d %H:%i:%s.%f')) AS solution_1,
DATE(try_cast('2017-03-29 10:32:28.0' as timestamp)) AS solution_2