Snowflake function how to input IN statment to function - snowflake-cloud-data-platform

I have function in Snowflake where I would like to input a variable there contains a IN statment for a query.
It works when I only have one item:
select * from table(my_function('62696'))
But I can not figure out how to do when I have more items, I have tried different ways like:
select * from table(my_function(''62696','62695''))
select * from table(my_function('\'62696\',\'62695\''))
But no of them does work.

I did solve it with by call the function with:
select * from table(my_function('62696,62695'))
In the function I use it like this:
IN (SELECT VALUE FROM TABLE(SPLIT_TO_TABLE(MY_INPUT, ',')))

Related

Extract string in snowflake

Is there a way in snowflake to do the followin
I want to provide input like below
'ab.cd#test.com,ef.gh#test.com,ij.kl.mn#test.com,op.qr#test.com'
output should be
ab.cd#test.com
That means, output would be starting from beginning before the first occurrence of "comma"
I am not sure if below code will work in all scenarios or there is a better way to do this in Snowflake
SELECT
SUBSTRING ('ab.cd#test.com,ef.gh#test.com,ij.kl.mn#test.com,op.qr#test.com', 1,
CHARINDEX (',', 'ab.cd#test.com,ef.gh#test.com,ij.kl.mn#test.com,op.qr#test.com')-1
)
Using SPLIT_PART function:
SELECT
SPLIT_PART('ab.cd#test.com,ef.gh#test.com,ij.kl.mn#test.com,op.qr#test.com', ',',1)
Output:
ab.cd#test.com
Alternatively SPLIT_TO_TABLE:
SELECT *
FROM TABLE(SPLIT_TO_TABLE('ab.cd#test.com,ef.gh#test.com,ij.kl.mn#test.com,op.qr#test.com', ',')) s
WHERE s.Index = 1;
Hi you can use SPLIT_PART
Reference: SPLIT_PART
https://docs.snowflake.com/en/sql-reference/functions/split_part.html#split-part
select split_part('ab.cd#test.com,ef.gh#test.com,ij.kl.mn#test.com,op.qr#test.com' ,
',',1) from dual;

Find rows where the value contains a word from a list of varchar(50)

I am collecting data from a t-sql stored procedure to import into c# program. I would like to narrow down the data first.
I have data that has three field that describes the three values that follows them. I need to find only the fields that have one of a dozen keywords in the description.
I was using something that UNION all the fields with values, then
...
AND (
TEXT1234.AccountValue LIKE '%word1%'
OR TEXT2345.AccountValue LIKE '%word1%'
OR TEXT3456.AccountValue LIKE '%word1%'
OR TEXT1234.AccountValue LIKE '%word2%'
OR TEXT2345.AccountValue LIKE '%word2%'
OR TEXT3456.AccountValue LIKE '%word2%'
OR TEXT1234.AccountValue LIKE '%word3%'
OR TEXT2345.AccountValue LIKE '%word3%'
OR TEXT3456.AccountValue LIKE '%word3%'
...
Now I am trying to do something like this:
declare #wordList table (Word varchar(50))
insert into #wordList values ('%word1%'),('%word2%'),('%word3%')...
...
SELECT *
FROM [DB1].dbo.Table_info
WHERE Account = 'TEXT1234'
AND AccountValue LIKE (SELECT * FROM #wordList)
...
(This is one of a number of similar UNION pieces. Which is why I would like to use a list of words, to shorten the code, and contain the words in one place in case of future changes.)
OR something Like:
SELECT *
FROM [DB1].dbo.Table_info
WHERE Account = 'TEXT1234'
AND AccountValue CONTAINS (SELECT * FROM #wordList)
...
Expected output:
TEXT1234_Account TEXT1234_AccountValue Acct1234 Acct1234_Value
TEXT1234 word3 something something ACCT1234 48
TEXT3456_Account TEXT3456_AccountValue Acct3456 Acct3456_Value
TEXT3456 Something word1 something ACCT3456 48
Please let me know if you need more code to analyze this...
(I am failing at keeping this brief already.)
Use EXISTS:
SELECT *
FROM [DB1].dbo.Table_info tbl
WHERE tbl.Account = 'TEXT1234'
AND EXISTS
(
SELECT 0
FROM #wordList wl
WHERE tbl.AccountValue LIKE wl.Word
)

Update with wildcard

in need to update all the rows that have sub string like this:
'forcestartpage=xx'and replace it with 'forcestartpage=18'
* there is lots of characters before and after the substring that shouldnt change
tried this, doesnt work:
update t_reminderscont
set body = REPLACE (body,'forcestartpage=__','forcestartpage=18')
thanks
You can get away with a STUFF function:
SELECT
T.body,
Replaced = STUFF(
T.Body, -- Insert in T.Body
CHARINDEX('forcestartpage=', T.Body), -- ... at the position where 'forcestartpage=' starts
LEN('forcestartpage=18'), -- ... while replacing 17 characters
'forcestartpage=18') -- ... the value forcestartpage=18
FROM
YourTable AS T
WHERE
T.body LIKE '%forcestartpage=__%' AND
T.body NOT LIKE '%forcestartpage=18%'
However this will only work for the first appearance of the forcestartpage= on each row.

Search for row where email contains only the X character

I want to search for rows whose column email has only the X character.
For example, if the email is XXXX, it should fetch the row but if the email is XXX#XXX.COM it should not be fetched.
I have tried something like this, but it is returning me all emails having the character X in it:
select *
from STUDENTS
where EMAIL like '%[X+]%';
Any idea what is wrong with my query?
Thanks
Try below query:
select *
from STUDENTS
where LEN(EMAIL) > 0 AND LEN(REPLACE(EMAIL,'X','')) = 0;
I would use PATINDEX:
SELECT * FROM STUDENTS WHERE PATINDEX('%[^X]%', Email)=0
Only X means no other characters than X.
To handle NULLs and empty strings you should consider additional conditions. See demo below:
WITH STUDENTS AS
(
SELECT * FROM (VALUES ('XXXX'),('XXX#XXX.COM'),(NULL),('')) T(Email)
)
SELECT *
FROM STUDENTS
WHERE PATINDEX('%[^X]%', Email)=0 AND LEN(Email)>0
This will find all rows where email only contain 1 or more X and no other characters.
SELECT *
FROM STUDENTS
WHERE Email not like '%[^X]%' and Email <> ''

SQL finding value

I would like to find F20300000000 in this string:
0xE90300000000EA0300000000EB0300000000EC0300000000ED0300000000EE0300000000EF0300000000F00300000000F10300000000F20300000000F30300000000F40300000000F60300000000F70300000000E90B00000000010C000000000D0C000000003E0C000000005E0C000000005F0C00000000630C00000000811B000000008B1B00000000951B000000009F1B00000000A91B00000000B31B00000000BD1B00000000C71B00000000
I've used already the wildcard like
LIKE '%F20300000000%' then I didn't get any result.
to make it clear, when my condition is true then it will show the name of the person who has the F20300000000 in their field, so my problem now is it seems I don't know how to find F20300000000 from the given value.
my query:
select C.Name
FROM
[SERVER01].[dbo].[character_table] AS C,
[SERVER01].[dbo].[achievement] AS T
WHERE C.CharacterIdx = T.CharacterIdx and T.AchievementData LIKE '%F20300000000%';
AchievementData Data Type is varbinary(4800)
Your column is likely to be binary so you should cast it to string:
select C.Name
FROM
[SERVER01].[dbo].[character_table] AS C,
[SERVER01].[dbo].[achievement] AS T
WHERE C.CharacterIdx = T.CharacterIdx and CONVERT(varchar(max), T.AchievementData, 2) LIKE '%F20300000000%';

Resources