Snowflake with regular expression - snowflake-cloud-data-platform

Trying to run the below SQL in Snowflake:
SELECT fm_id ,
CASE
WHEN regexp_instr(ASSD,'...',1) > 0
THEN regexp_SUBSTR(ASSD,1,regexp_instr(ASSD,'...',1)-1)
ELSE ASSD
END ASSD
from
(SELECT a.fm_id,
listagg(a.STUID, '; ') within GROUP (
ORDER BY a.Fm_id, a.STUID ) ASSD
from stu_d a
where fm_id = 1222
group by a.fm_id
)
Getting error:
"Invalid parameter value: 0. Reason: Position must be positive"
seems it is failing at -1 or 0 value in above case statement.
What am I doing wrong?

Without seeing your table it's hard to say, but regexp_instr() will return 1 when the pattern is at the beginning of the string. Then, you subtract 1, and 0 is an invalid position argument to regexp_substr(). doc
Perhaps you intended to use SUBSTR not REGEX_SUBSTR()

Related

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() );

SQL CASE when text field is NULL not working

I'm trying to run the following case statement in SQL:
CASE
WHEN P.Photos_Cloned_From IS NULL
THEN 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.ProductCode+'-2T.jpg'
ELSE 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.Photos_Cloned_From+'-2T.jpg'
END AS image_link,
The statements works, but only for those records that are not NULL. For items that are NULL, the statement is not returning the THEN condition.
Any suggestions?
Try this and let us know what happens.
'http://www.toolboxbarn.com/v/vspfiles/photos/'+
COALESCE(P.Photos_Cloned_From,P.ProductCode,'DEFAULT')+'-2T.jpg'
AS image_link,
Using coalesce here is better than the case statement. Some platforms can optimize coalesce and it lets you easily make a default value.
#hogan's Suggestion is a great one to save code, but ultimately it should have the same result as your case statement if you don't introduce his new 'default' case. It is most likely that the ProductCode is also NULL is that a possibility?
Why string + NULL = NULL because NULL is an unknown in sql most db platforms will nullify the entire value when null is aggregated or concatenated.
So Think of the following test cases:
Id ProductCode Photos_Cloned_From
1 1 ClonedFrom
2 2 Null
3 NULL Null
The results of your case expression and Hogan's Suggestion would be:
1) ELSE 'http://www.toolboxbarn.com/v/vspfiles/photos/ClonedFrom-2T.jpg'
2) WHEN 'http://www.toolboxbarn.com/v/vspfiles/photos/2-2T.jpg'
3) WHEN NULL
CASE WHEN ISNULL(P.Photos_Cloned_From,'') = '' THEN 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.ProductCode+'-2T.jpg'
ELSE 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.Photos_Cloned_From+'-2T.jpg'
END AS image_link,
SELECT
F.Name
, F.Address
, F.InDate
, ( CASE WHEN (F.Date_Down IS NULL and F.Cod_Down = 0 )THEN 0 ELSE -1 END) as 'sn_Down'
FROM
Folders F

Select Records Expert equivalent for SSRS

Crystal Reports has the Select Records Expert which allows you to use variables to build WHERE conditions. Is there something similar to the select records experts in SSRS?
If the variable #Restriction below is equal to one, the column name CFE_EDI.ETAT = 'ENV' or CFE_EDI.ETAT = 'OUV'. I tried to mimic the functionality with a SQL query but due to AND and OR precedence, the WHERE condition is not functioning properly.
You can handle this within the SQL of your SSRS Dataset where clause buy using case statements that return a 1 or a 0 depending on whether or not the criteria is met:
where case when #Restriction = '1'
then case when CFE_EDI.ETAT in('ENV','OUV') then 1 else 0 end
when #Restriction = '0'
then case when CFE_EDI.ETAT not in('ENV','OUV') then 1 else 0 end
else case when CFE_EDI.ETAT <> '' then 1 else 0 end
end = 1

returns 0 rows when converting old code to Tsql

I am translating a query from some other platform to T-SQL.
I was told as, I went along in SSMS what code was invalid, so I think I changed everything, and now the code runs with the output of 0 Rows affected. The old code finds rows.
Anything obviously wrong with this code?
SELECT 'invalid_character_in_prod_ID ',
a.client_id,
a.prod_id
FROM tsc.ems_tvc_prod a,
tsc.ems_tvc_clnt_bus c
WHERE a.client_id = c.client_id
AND a.bus_unit = c.bus_unit
AND ts_sub_org = 'FORD'
AND ( Patindex(prod_id, Char(9)) > 0
OR Patindex(prod_id, Char(160)) > 0 )
Your use of PATINDEX is incorrect.
Try:
AND ( Patindex('%'+Char(9)+'%', prod_id) > 0
OR Patindex('%'+Char(160)+'%', prod_id) > 0 )
...or just use CHARINDEX as #MartinSmith suggests.

Arithmetic overflow error converting nvarchar to data type numeric

I have a stored procedure which works fine in my local environment as well as the QA environment.
However at the UAT environment at the client it gives the error
System.Data.SqlClient.SqlException:
Message number="8115" severity="16" state="8">Arithmetic overflow error
converting nvarchar to data type numeric.
It also works fine in one of the local instances installed in the client location.
I found out the line of code which gives an error by commenting/umcommenting lines of code and zeroed it down to
(
#TotalHHInternalTo IS NULL
OR
(
IsNumeric(E.[Xml].value(
'declare default element namespace "http://www.xyz/1.0";
(/Event/Data/CustomData/DataXML/ProductData/ProductParty/ProductCategory[#code != ''Protection'']/Product/FundValue)[1]',
'nvarchar(50)'
) ) = 1
AND
EXISTS
(
SELECT
1
FROM E.[Xml].nodes(
'declare default element namespace "http://www.xyz/1.0";
/Event/Data/CustomData/DataXML/ProductData/ProductParty/ProductCategory[#code != ''Protection'']/Product') as P(E)
HAVING SUM(P.E.value(
'declare default element namespace "http://www.xyz/1.0";
(FundValue)[1]',
'decimal'
)) <= #TotalHHInternalTo
)
)
)
The variable #TotalHHInternalTo is a parameter of type decimal which is part of search criteria passed to the stored procedure as an xml. Under Product party I have 4 product category and I need to total up all Fund value except of type Protection. If the sum of these 3 types is less than #TotalHHInternalTo I would like to display it in the search result.
I added the isNumeric condition to check if the value picked up from the xml is a numeric value.
However I still get an overflow error.
Check my brackets, but you need to convert it to explicit short-circuit boolean evaluation using CASE. SQL Server is free to evaluate AND conditions in either order, and you got the wrong end of the stick.
CASE WHEN IsNumeric(E.[Xml].value(
'declare default element namespace "http://www.focus-solutions.co.uk/focus360/1.0";
(/Event/Data/CustomData/DataXML/ProductData/ProductParty/ProductCategory[#code != ''Protection'']/Product/FundValue)[1]',
'nvarchar(50)'
) ) = 0
THEN 0
WHEN
EXISTS
(
SELECT
1
FROM E.[Xml].nodes(
'declare default element namespace "http://www.focus-solutions.co.uk/focus360/1.0";
/Event/Data/CustomData/DataXML/ProductData/ProductParty/ProductCategory[#code != ''Protection'']/Product') as P(E)
HAVING SUM(P.E.value(
'declare default element namespace "http://www.focus-solutions.co.uk/focus360/1.0";
(FundValue)[1]',
'decimal'
)) <= #TotalHHInternalTo
)
THEN 1
ELSE 0 END = 1

Resources