Need help in using Regexp - snowflake-cloud-data-platform

SELECT value from table1 limit 3;
When I run the above query, I'm getting the below results:
;12770258;;;;103=::dupe::0|112=::dupe::0|114=search-results#product-id#Lettuce#not-recommended#r02#s01
;12880258;;;;103=::dupe::0|112=::dupe::0|114=search-results#product-id#Lettuce#not-recommended#r02#s01
;12990258;;;;103=::dupe::0|112=::dupe::0|114=search-results#product-id#Lettuce#not-recommended#r02#s01
I want the results to be displayed as below:
12770258
12880258
12990258
How to implement this using Regular Expression? Kindly guide.

We can use REGEXP_SUBSTR here:
SELECT REGEXP_SUBSTR(value, '\\d+', 1, 1, 'i', 1) AS output
FROM yourTable;
The above call to REGEXP_SUBSTR finds the first digit in the column, starting the search at the start of the column. In regex, \d+ matches a group of one or more digit characters.

you could just use SPLIT if you know it's always the "second" value in the series. If that is not true, then the REGEPX version will not help you ether.
SELECT ';12770258;;;;103=::dupe::0|112=::dupe::0|114=search-results#product-id#Lettuce#not-recommended#r02#s01' as col1
,split(col1,';')[1]::text;
COL1
SPLIT(COL1,';')[1]::TEXT
;12770258;;;;103=::dupe::0|112=::dupe::0|114=search-results#product-id#Lettuce#not-recommended#r02#s01
12770258

Related

Remove all letters from a Code in SSIS in Derived column transformation editor

I have these codes in SSIS:
102022A
1035C
102074A
102074D
174Z
I would like to remove the letters (last string) of my codes to return the data like this:
102022
1035
102074
102074
174
How I can do this in SSIS? Which proper function should I use?
Assuming we can rely on there only being one letter, which also always occurs at the end of the code, we can use SUBSTRING here:
SELECT code, SUBSTRING(code, 1, LEN(code) - 1) AS code_out
FROM yourTable;
If you actually want to modify the codes in your table, then use an update with similar logic:
UPDATE yourTable
SET code = SUBSTRING(code, 1, LEN(code) - 1)
WHERE code LIKE '%[A-Z]';

SQL Server Full text search Contains function

İ have a problem with contains function, when i search with like '%ZAM%' operator, it finds all word that contains ZAM like ZAMANLAMA AZAMI ZAM and etc.. but when I use fts index contains function, it just find ZAM ZAMANLAMA but it doesnt find AZAMI or 123ZAM789. I have also tried CONTAINS (YourColumn, ' "ZAM" ' ) but it doesn't work. Please help me , fts is very fast but it could not find all contains like '%%' operator what should I do ?
You can use "*" before in contain syntax same as like operator . but you need to use double quote before and after the search string.
Try this query once.
SELECT *
FROM YourTable
WHERE CONTAINS(YourColumn,'"*ZAM*"');
(OR)
select * from YourTable where YourColumn like '%ZAM%'

Crystal Reports command not working

I have a crystal report connected to a table with a command which has parameters, this command query is working
SELECT
date1,
car_num,
ar_dromologiou,
Sum(kilomtres) As ksum,
sum(posostita_ltitra) as sumpos,
sum(shmaia) as sumkom,
sum(sinokomsim) AS x3
FROM
Table_1
WHERE
date1 BETWEEN {?d1} AND {?d2}
GROUP BY
date1,
car_num,
ar_dromologiou;
this doesn't work
SELECT
date1,
car_num,
ar_dromologiou,
Sum(kilomtres) As ksum,
sum(posostita_ltitra) as sumpos,
sum(shmaia) as sumkom,
sum(sinokomsim) AS x3
FROM
Table_1
WHERE
car_num LIKE {?c1} AND
date1 BETWEEN {?d1} AND {?d2}
GROUP BY
date1,
car_num,
ar_dromologiou;
Where is the fault?
Thank you all for the answers , I found that the values where in non english characters and crystal Reports wants some Extra configuration for this, didn't find yet but when I insert a record with english characters and in the parameter input english chars it showed the record, so something about the character encoding.
The problem seems to be in your where clause where you compare car_num.
If you pass in your parameter {?c1} and it doesn't include any wildcard charaters like % or _ you will only get exact matches.
Use wildcard characters appropriate for your use case, e.g.
WHERE
car_num LIKE {?c1} + '%' AND
date1 BETWEEN {?d1} AND {?d2}
This will match all rows where car_num starts with the value of your parameter {?c1}.
You may want to have a look at the documentation for the LIKE operator.

Concat the values in a string with SQL Server

I want to select a list of items and part numbers for for each item as a string:
SELECT top 100 *
FROM ii
OUTER APPLY
(SELECT def, ( ipr.part_number + ',') as prt
FROM ipr
WHERE ii.item_id = ipr.item_id
FOR XML PATH('') ) PN
The error is:
[Error Code: 8155, SQL State: S0002] No column name was specified for
column 1 of 'PN'.
How can I fix this?
I think that your whole OUTER APPLY statement generates one XML for both default_part_number and concatenated string, which(the whole XML) doesn't have a name.
What you could try to do would be adding alias like this AS PN(TestThis).
However, I don't think that you're expecting result you're going to get. It would better if you'd give us some example data and expected output. It will be easier for us to solve your problem in that case.
The combination of XML and STUFF is funny but perfectly fitting to your needs.
First you concat your strings with the ', ' in front, then you must return your XML with ", TPYE). You must read the result with ".value()" and use STUFF to replace the first ', '.
You'll find a lot of exampels in the net...

SQL Server String extract based on pattern

I have string data in the following format:
MODELNUMBER=Z12345&HELLOWORLD=WY554&GADTYPE=PLA&ID=Z-12345
/DTYPE=PLA&ID=S-10758&UN_JTT_REDIRECT=UN_JTT_IOSV
and need to extract IDs based on two conditions
Starting after a pattern &ID=
Ending till the last character or
if it hits a & stop right there.
So in the above example I'm using the following code:
SUBSTRING(MyCol,(PATINDEX('%&id=%',[MyCol])+4),(LEN(MyCol) - PATINDEX('%&id%',[MyCol])))
Essentially looking the pattern &id=% and extract string after that till end of the line. Would anyone advise on how to handle the later part of the logic ..
My current results are
Z-12345
Z-12345&UN_JTT_REDIRECT=UN_JTT_IOSV
What I need is
Z-12345
Z-12345
Try this
SUBSTRING(MyCol, (PATINDEX('%[A-Z]-[0-9][0-9][0-9][0-9][0-9]%',[MyCol])),7)
if you run into performance issues add the where clause below
-- from Mytable
WHERE [MyCol] like '%[A-Z]-[0-9][0-9][0-9][0-9][0-9]%'
maybe not the most elegant solution but it works for me.
Correct syntax of PATINDEX
Here's one example how to do it:
select
substring(d.data, s.s, isnull(nullif(e.e,0),2000)-s.s) as ID,
d.data
from data d
cross apply (
select charindex('&ID=', d.data)+4 as s
) s
cross apply (
select charindex('&', d.data, s) as e
) e
where s.s > 4
This assumes there data column is varchar(2000) and the where clause leaves out any rows that don't have &ID=
The first cross apply searches for the start position, the second one for the end. The isnull+nulliff in the actual select handles the case where & is not found and replaces it with 2000 to make sure the whole string is returned.

Resources