DATE Conversion in Snowflake does not work within CASE statement - snowflake-cloud-data-platform

I am trying to achieve this
CASE
WHEN
(DIM_TEST."TEST START DATE" <> ''
OR DIM_TEST."EST START DATE" IS NOT NULL
OR DIM_TEST."EST START DATE" <> 'NULL')
THEN TO_VARCHAR(TO_DATE(DIM_TEST."TEST START DATE", 'YYYYMMDD'), 'YYYY-MM-DD')
ELSE DIM_TEST."TEST START DATE"
END AS "FINAL_DATE"
It does not work
too many arguments for function [TO_DATE(DIM_TEST."TEST START DATE", 'YYYY-MM-DD')] expected 1, got 2
Any suggestions?
Thank You

The error indicates that the source column data type is not string but rather DATE/TIMESTAMP
SELECT TO_DATE(CURRENT_DATE(), 'YYYYMMDD')
-- Error: too many arguments for function [TO_DATE(CURRENT_DATE(), 'YYYYMMDD')] expected 1, got 2 (line 1)
The TO_DATE allows to use second argument only if the first argument is string:
TO_DATE( <string_expr> [, <format> ] )
TO_DATE( <timestamp_expr> )
TO_DATE( '<integer>' )
TO_DATE( <variant_expr> )
The entire CASE expression is probably not correct as the column is not string, and there are comparison against string literals '' and 'NULL'.
Finally, the first part of case expression tries to return stringTO_VARCHAR but the second is an original column, thus the final data type will be the data type of the column.

Related

My case statement is returning 1900-01-01 for empty dates

I am trying to return a result for 2 date columns based on different values like this:
CASE WHEN game_startA IS NOT NULL THEN game_startA
WHEN game_startB IS NOT NULL THEN game_startB
ELSE ''
END AS 'Game Start Date'
,
CASE WHEN game_endA IS NOT NULL THEN game_endA
WHEN game_endB IS NOT NULL THEN game_endB
WHEN COALESCE(game_endC, game_endD) IS NOT NULL THEN COALESCE(game_endC, game_endD)
ELSE ''
END AS 'Game End Date'
The problem is, if the value is NULL, it is returning 1900-01-01 instead of saying NULL or just blank.
Is there a way to fix that?
This logic could be vastly simplified (unless there is more to it than you've shared):
COALESCE(game_startA, game_startB) AS [Game Start Date],
COALESCE(game_endA, game_endB, game_endC, game_endD) AS [Game End Date]
I don't see any need for the CASE expressions or any ELSE that tries to turn a date into an empty string. If COALESCE() gets to the end of its list and still doesn't find a non-NULL value, the output is what you want: NULL.

Snowflake Numeric value '' is not recognized

Please help me with error 'Numeric value '' is not recognized'.
In Snowflake i am having a variable day_minus which holds a number, using this i want to return date value of current_date minus the value given in the variable.
If there is Null or empty passed in the variable i want to get current date minus 1.
For this i have written a code like below. But it throws me an error Numeric value '' is not recognized
But it throws an
set day_minus=7;
SELECT DATEADD(DAY, concat('-',nvl(nullif(try_to_number($day_minus) ,''),1)),current_date() );
Can you please correct me where i am doing mistake
you can use case statement
SELECT DATEADD(DAY, concat('-',nvl(case when $Units ='' then 1 else $Units end ,1)),current_date() );
If you use concat then the result is always going to be a string. If you want to set the "default" value to -1 then just put -1 in your code.
The try_to_* functions take a string as their input so you'd need to make your variable a string to make this work e.g.
set day_minus='7';
You don't need nullif at all. So the final version would be something like:
set day_minus='7';
DATEADD(DAY, nvl(try_to_number($day_minus),-1),current_date() );

invalid input syntax for type timestamp

While running the below code i get an error saying invalid input syntax for type timestamp from admission_datetime.
UPDATE ccsm.stg_demographics_baseline
SET xx_los_days =
(CASE WHEN admission_datetime IS NULL OR
date_trunc('day',admission_datetime) = ''
THEN NULL
WHEN discharge_datetime IS NULL OR
date_trunc('day',discharge_datetime) = ''
THEN date_diff('day', admission_datetime, CURRENT_DATE)
ELSE
date_diff('day', admission_datetime, discharge_datetime)
END);
enter code here
See date_trunc documentation:
The return value is of type timestamp or interval with all fields that are less significant than the selected one set to zero (or one, for day and month).
So you can not compare it with an empty string:
date_trunc('day', admission_datetime) = ''
The invalid input syntax for type timestamp error message concerns the empty string (''), not the admission_datetime column.
Furthermore, there is no date_diff function in PostgreSQL. Just subtract one timestamp from another and you will get an interval result:
SELECT timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00'
You'll get
interval '1 day 15:00:00'
If you need the difference in days, try this:
SELECT DATE_PART('day', timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00')
The result is 1.
See here for examples of DATEDIFF-like expressions in PostgreSQL.

Stored Procedure Date time error

I wrote an sp basically to check the diff between dates If the diff is either 4 or 2 then the sp runs and raises an error it not.
CREATE PROCEDURE AML_DATECHECK
#ALERTDAY VARCHAR
AS
IF((SELECT "DAY_DESC" =
CASE WHEN DATEDIFF(DAY,CONVERT(VARCHAR,"BUSINESS_DAY",120),
CONVERT(VARCHAR,CONVERT(VARCHAR,'#ALERTDAY',121),120))= 4
THEN 'BUSINESS DAY IS FRIDAY'
WHEN DATEDIFF(DAY,CONVERT(VARCHAR,"BUSINESS_DAY",120),
CONVERT(VARCHAR,CONVERT(VARCHAR(25),'#ALERTDAY',121),120)) = 2
THEN 'BUSINESS DAY IS' + datename(dw,"BUSINESS_DAY")
END
FROM [US_AML_APP].[dbo].[SAM_APP_USER_PROCESS] WHERE
[PROCESS_NAME]='AML_SAM_dailyProcess') IS NULL )
RAISERROR('DATE CHECK FAILED',11,1)
ELSE
RAISERROR('DATE CHECK PASSED',12,2)
RETURN
When I try to pass a parameter like here
exec AML_DATECHECK #ALERTDAY='2015-11-23'
it fails.
I have also tried all the date formats such as 2015/23/11 and tried giving all the datatypes but the execution fails with:
Conversion failed when converting date and/or time from character
string.
Could anyone help me?

Why is my CASE statement requiring the THEN part to be of data type INT?

I am trying to run a query where the below CASE statement is one of the lines. I'm using Report Builder 3.0.
However, I get an error that says :
Conversion failed when converting the varchar value 'Case 1' to data type int; Microsoft SQL Server, Error: 245".
Why is the CASE statement requiring the THEN part to be an INT data type ?
(CASE
WHEN jobs.Uf_Production_Line = 'LN BM6'
THEN 'Case 1'
ELSE
99999
END
) AS line
When using CASE statement, all result expressions must have the same data type. If not, the result will be converted to the data type with a higher precedence. According to BOL
Returns the highest precedence type from the set of types in
result_expressions and the optional else_result_expression.
And since INT has a higher data type precedence than VARCHAR, your query produces an error:
Conversion failed when converting the varchar value 'Case 1' to data
type int; Microsoft SQL Server, Error: 245".
To fix this, you should convert your ELSE part to VARCHAR.
ELSE '99999'
Because your else uses a value with data type int:
ELSE 99999
You use data types (varchar and int) that can't be exchanged automatically.
An option is to use:
ELSE '99999'
You should use '99999' instead of 99999. All values should be the same datatype in CASE, for now in your ELSE part values is of INT datatype.
Full code should be like:
(CASE WHEN jobs.Uf_Production_Line = 'LN BM6' THEN 'Case 1'
ELSE '99999'
END
) AS line
A case statement can only return one data type. So use the number as varchar:
(CASE WHEN jobs.Uf_Production_Line = 'LN BM6'
THEN 'Case 1'
ELSE '99999'
END
) AS line
OR you can return NULL instead of 9999 if you want to represent it as an invalid value for Line:
(CASE WHEN jobs.Uf_Production_Line = 'LN BM6'
THEN 'Case 1'
ELSE NULL
END
) AS line
Because 99999 is an int. Both cases must return the same data type.

Resources