Extract string in snowflake - snowflake-cloud-data-platform

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;

Related

how to use a sql substring in a where in string list conditional

I have a where conditional
where
pnumber in ('mem1234', 'mem2345','mem8978')
I need a substring without the mem in this where conditional. There are a couple hundred strings in this where conditional.
What is the syntax for this?
In order to manipulate the data you will first need to insert it into a temp table/ table variable and then depending on how consistant your data is you could try the following:
DECLARE #pNumbers TABLE (item varchar(20))
insert into #pNumbers values('mem1234'),('mem2345'),('mem8978')
select
REPLACE(item, 'mem', '') AS ReplaceMethod ,
SUBSTRING(item, 4, 4) AS SubstringMethod,
RIGHT(item,LEN(item) - 3) AS Right_LenMethod
FROM #pNumbers
You would then add one of them methods to a subquery in the WHERE
WHERE
pnumber IN ( SELECT REPLACE(item, 'mem', '') FROM #pNumbers )
Or as others have stated, you could open the csv in an external program and find and replace but atleast you have options

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.

Convert Statement to Crystal Reports SQL Expression

I have a SQL command that works great in SQL Server. Here's the query:
SELECT TOP 1000
(
SELECT COUNT(LINENUM)
FROM OEORDD D1
WHERE D1.ORDUNIQ = OEORDD.ORDUNIQ
)
- (SELECT COUNT(LINENUM)
FROM OEORDD D1
WHERE D1.ORDUNIQ = OEORDD.ORDUNIQ
AND D1.LINENUM > OEORDD.LINENUM)
FROM OEORDD
ORDER BY ORDUNIQ, LINENUM
The query looks at the total lines on an order, then looks at the current "LINENUM" field. With the value of the LINENUM field, it looks to see how many lines have a greater LINENUM value on the order and subtracts it from the number of lines on an order to get the correct Line number.
When I try to add it as a SQL expression in version 14.0.2.364 as follows:
(
(
SELECT COUNT("OEORDD"."LINENUM")
FROM "OEORDD" "D1"
WHERE "D1"."ORDUNIQ" = "OEORDD"."ORDUNIQ"
)
- (SELECT COUNT("OEORDD"."LINENUM")
FROM "OEORDD" "D1"
WHERE "D1"."ORDUNIQ" = "OEORDD"."ORDUNIQ"
AND "D1"."LINENUM" > "OEORDD"."LINENUM"
)
)
I get the error "Column 'SAMDB.dbo.OEORDD.ORDUNIQ' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If I try to add GROUP BY "OEORDD"."ORDUNIQ" at the end, I get "Incorrect syntax near the keyword 'GROUP'. I've tried adding "FROM OEORDD" at the end of query and it errors out on the word "FROM". I have the correct tables linked in the Database Expert.
EDIT --------------
I was able to get the first query working by getting rid of the alias, it's as follows:
(
SELECT COUNT(LINENUM)
FROM OEORDD
WHERE OEORDH.ORDUNIQ=OEORDD.ORDUNIQ)
)
However, I believe I need to use the alias in the second query to compare line numbers. I'm still stuck on that one.

replace not working after specific number

I have a table with a column vouchn. The recod of this column eg-if it receipt voucher it will record like RV103 AND LIKE payment it stores like PV99. I also use this sql for gettin max records.
SELECT MAX(REPLACE(vouchn, 'RV', '')) AS vcno
FROM dbo.dayb
WHERE (vouchn LIKE '%RV%')
it is ok until i reach RV999. After then even record RV1000 is there the above sql retrieve RV999. What is the error of the above code.
If REPLACE(vouchn, 'RV', '') always return numeric result, you can as the below:
SELECT MAX(REPLACE(vouchn, 'RV', '') * 1) AS vcno
FROM dbo.dayb
WHERE (vouchn LIKE '%RV%')
Add CONVERT to numeric type:
SELECT MAX(CONVERT(BIGINT,REPLACE(vouchn, 'RV', ''))) AS vcno
FROM dbo.dayb
WHERE (vouchn LIKE '%RV%')

Oracle split text into multiple rows

Inside a varchar2 column I have text values like :
aaaaaa. fgdfg.
bbbbbbbbbbbbbb ccccccccc
dddddd ddd dddddddddddd,
asdasdasdll
sssss
if i do select column from table where id=... i get the whole text in a single row, normally.
But i would like to get the result in multiple rows, 5 for the example above.
I have to use just one select statement, and the delimiters will be new line or carriage return (chr(10), chr(13) in oracle)
Thank you!
Like this, maybe (but it all depends on the version of oracle you are using):
WITH yourtable AS (SELECT REPLACE('aaaaaa. fgdfg.' ||chr(10)||
'bbbbbbbbbbbbbb ccccccccc ' ||chr(13)||
'dddddd ddd dddddddddddd,' ||chr(10)||
'asdasdasdll ' ||chr(13)||
'sssss '||chr(10),chr(13),chr(10)) AS astr FROM DUAL)
SELECT REGEXP_SUBSTR ( astr, '[^' ||chr(10)||']+', 1, LEVEL) data FROM yourtable
CONNECT BY LEVEL <= LENGTH(astr) - LENGTH(REPLACE(astr, chr(10))) + 1
see: Comma Separated values in Oracle
The answer by Kevin Burton contains a bug if your data contains empty lines.
The adaptation below, based on the solution invented here, works. Check that post for an explanation on the issue and the solution.
WITH yourtable AS (SELECT REPLACE('aaaaaa. fgdfg.' ||chr(10)||
'bbbbbbbbbbbbbb ccccccccc ' ||chr(13)||
chr(13)||
'dddddd ddd dddddddddddd,' ||chr(10)||
'asdasdasdll ' ||chr(13)||
'sssss '||chr(10),chr(13),chr(10)) AS astr FROM DUAL)
SELECT REGEXP_SUBSTR ( astr, '([^' ||chr(10)||']*)('||chr(10)||'|$)', 1, LEVEL, null, 1) data FROM yourtable
CONNECT BY LEVEL <= LENGTH(astr) - LENGTH(REPLACE(astr, chr(10))) + 1;

Resources