How do I fix this code to make it work in HIVE? I tried multiple combinations of () but nothing works…
Where (DOB is not NULL)
and (DATE_FORMAT(DOB, 'yyyyMM') BETWEEN 201601 AND 201805)
and (color in ('orange', ‘blue', 'pink'))
(DOB is not NULL) condition is redundant, because if DATE_FORMAT(DOB, 'yyyyMM') BETWEEN 201601 AND 201805 then DOB is definitely NOT NULL.
So this should work (fix wrong single-quote near blue):
Where (DATE_FORMAT(DOB, 'yyyyMM') BETWEEN 201601 AND 201805)
and (color in ('orange', 'blue', 'pink'))
date_format works only if first argument is correct date or timestamp: yyyy-MM-dd HH:mm:ss.SSSS, if not, use something else to extract yyyyMM
Related
I need some help around BigQuery date formatting based on their values. The source data has STRING datatype and target datatype is DATE. I need to format the input dates based on their values as follow:
NULL value should remain NULL
Empty string("") should be convert to NULL
Date with YYYY-MM-DD format should remain as is
Date with MM/DD/YYYY format should be convert to YYYY-MM-DD format
Here's what I have done so far:
SELECT input_date, CASE WHEN input_date = '' THEN NULL ELSE PARSE_DATE('%m/%d/%Y', input_date) END AS output_date FROM mytable
The above case statement fails when try to parse the dates with YYYY-MM-DD format. Here's the error I am getting:
How do I solve for the YYYY-MM-DD date format? Any feedback is appreciated.
You may try and consider below approach wherein you will need to add another WHEN and then use regex to match the YYYY-MM-DD string and then parse it to your desired date format as shown below.
with sample_data as (
select NULL as input_date,
union all select '' as input_date,
union all select '2022-05-21' as input_date,
union all select '05/25/2022' as input_date
)
SELECT input_date,
CASE WHEN input_date = '' THEN NULL
WHEN REGEXP_CONTAINS(input_date, r'^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$') THEN PARSE_DATE('%F', input_date)
ELSE PARSE_DATE('%m/%d/%Y', input_date) END AS output_date FROM sample_data
Output:
Consider below query:
WITH sample_data AS (
SELECT * FROM UNNEST(['', null, '2022-05-21', '05/25/2022']) input_date
)
SELECT *,
COALESCE(SAFE.PARSE_DATE('%m/%d/%Y', input_date), SAFE.PARSE_DATE('%F', input_date)) output_date
FROM sample_data;
Output results
Below is a script i am trying to run in Presto; Subtracting today's date from an integer field I am attempting to convert to date. To get the exacts days between. Unfortunately, it seems the highlighted block does not always convert the date correctly and my final answer is not correct. Please does anyone know another way around this or a standard method on presto of converting integer values to date.
Interger value in the column is in the format '20191123' for year-month-date
select ms, activ_dt, current_date, date_diff('day',act_dt,current_date) from
(
select ms,activ_dt, **CAST(parse_datetime(CAST(activ_dt AS varchar), 'YYYYMMDD') AS date) as act_dt**, nov19
from h.A_Subs_1 where msisdn_key=23480320012
) limit 19
You can convert "date as a number" (eg. 20180527 for May 27, 2018) using the following:
cast to varchar
parse_datetime with appropriate format
cast to date (since parse_datetime returns a timestamp)
Example:
presto> SELECT CAST(parse_datetime(CAST(20180527 AS varchar), 'yyyyMMdd') AS date);
_col0
------------
2018-05-27
You can use below sample query for your requirement:
select date_diff('day', date_parse('20191209', '%Y%m%d'), current_timestamp);
I am comparing the day of week values to their string literal equivalents, but am consistently getting false. I have a relatively straightforward string DoW to 'monday' comparison setup in SQL Fiddle, however, only the DoW to int compare works. I plan to use that going forward, but I am trying to understand what is failing.
http://sqlfiddle.com/#!17/2d52d/29
day1 like day2 AS like_compare, //Expect true, get false
day1 = day2 AS equal_compare, //Expect false, get false
day1::text like day2 AS text_compare, //Expect true, get false
EXTRACT(DOW FROM '2019-08-05') = 1 AS value_compare //Expect true, get true
I am trying to understand what is happening that is causing PostgreSQL to act this way. As you can see in the fiddle, none of the relevant string literals have an 'unknown' type. I accept that the correct way to compare days of weeks is to use the 'value' compare, however, that doesn't explain what is happening with the 'like' and 'text' compares.
The difference stems from extract and to_char handling Day/DOW requests slightly differently. See EXTRACT DOW and TO_CHAR. In this particular case try trim on day1.
WITH mySelect AS
( SELECT
to_char(day1, 'day') AS day1,
day2
FROM
myTable
)
SELECT
*,
pg_typeof(day1)::text like pg_typeof(day2)::text AS types_match,
trim(day1) like day2 AS like_compare,
trim(day1) = day2 AS equal_compare,
trim(day1::text) like day2 AS text_compare,
EXTRACT(DOW FROM( SELECT day1 FROM myTable)) = 1 AS value_compare,
CAST('monday' AS text) like CAST('monday' AS text) AS sanity_compare
FROM
mySelect;
Novice to postgresql here. On 8.3 (not my choice, can't do anything about it for near future).
I'm selecting some "time" text strings from a table and inserting them into a view:
create or replace view test as (
select
case
when desc like '%opening%' then 'opening'
when desc like '%closing%' then 'closing'
else n/a
end as time_type,
to_timestamp(time, 'MM/DD/YYYY HH:MI:SS AM') where time_type = 'closing' as closing_time,
to_timestamp(time, 'MM/DD/YYYY HH:MI:SS AM') where time_type = 'opening' as opening_time
from source_table);
I get the error:
ERROR: syntax error at or near "as"
LINE 8: .../YYYY HH:MI:SS AM') where time_type = 'closing' as closing...
I've used this syntax before to create other views so I am confused. Is it because my where statements are incorrectly placed? But if I place them after the from, they will be applied universally, no, and that is not what I want.
Two expressions look like they should be case statements, e.g.:
case
when time_type = 'closing' then
to_timestamp(time, 'MM/DD/YYYY HH:MI:SS AM')
else null
end as closing_time
Convert the two projections beginning with to_timestamp to CASE statements as well.
Help me Stackoverflow, I'm close to going all "HULK SMASH" on my keyboard over this issue. I have researched carefully but I'm obviously not getting something right.
I am working with a Julian dates referenced from a proprietary tool (Platinum SQL?), though I'm working in SQL 2005. I can convert their "special" version of Julian into datetime when I run a select statement. Unfortunately it will not insert into a datetime column, I get the following error when I try:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
So I can't setup datetime criteria for running a report off of the Stored Procedure.
Original Value: 733416
Equivalent Calendar Value: 01-09-2009
Below is my code... I'm so close but I can't quite see what's wrong, I need my convert statement to actually convert the Julian value (733416) into a compatible TSQL DATETIME value.
SELECT
org_id,
CASE WHEN date_applied = 0 THEN '00-00-00'
ELSE convert(char(50),dateadd(day,date_applied-729960,convert(datetime, '07-25-99')),101)
END AS date_applied,
CASE WHEN date_posted = 0 THEN '00-00-00'
ELSE convert(char(50),dateadd(day,date_posted-729960,convert(datetime, '07-25-99')),101)
END AS date_posted
from general_vw
SELECT
org_id,
CASE WHEN date_applied = 0 OR date_applied < 639906 THEN convert(datetime, '1753-01-01')
ELSE dateadd(day,date_applied-729960,convert(datetime, '07-25-99'))
END AS date_applied,
CASE WHEN date_posted = 0 OR date_applied < 639906 THEN convert(datetime, '1753-01-01')
ELSE dateadd(day,date_posted-729960,convert(datetime, '07-25-99'))
END AS date_posted
from general_vw
You're casting to char but want a datetime so that's one easy fix.
You were also using '00-00-00' as your minimum date, but the minimum TSQL date is '1753-01-01'. Alternatively you could use something like ('1900-01-01') but that would need a change to the "less than" date_applied comparer.
I've added a "less than" date_applied comparer too. I calculated this as "SELECT 729960 + datediff(day,convert(datetime, '07-25-99'), convert(datetime,'1753-01-01'))". Any number less than this would cause a date underflow.