Need guidance in using REGEXP - snowflake-cloud-data-platform

SELECT ID, web_name from table_name WHERE date='2022-02-02'
AND web_name LIKE '%https:%'
ID WEB_NAME
1234 OFFERhttps://www.google.com/
3456 THEMEhttps://www.google.com/
When I run the above query, I'm getting the above results. I want the results to be displayed as
below:
ID WEB_NAME
1234 OFFER
3456 THEME
I want https://www.google.com/ to be removed from the results. Any guidance would be of great help..

Something like this should do it:
SELECT REGEXP_REPLACE('OFFERhttps://www.google.com/', 'http.*$', '');
I get the output as:
OFFER
Same when I run it for the other row. Docs here

You can use RGEXP_SUBSTR to match the "everything before http:", and depending if you want case insensitive matching (solution_1) or not (solution_2)the i clause should be added:
SELECT column1 as ID
,column2 as web_name
,REGEXP_SUBSTR(web_name, '^(.*)https\:',1,1,'ie') as solution_1
,REGEXP_SUBSTR(web_name, '^(.*)https\:',1,1,'e') as solution_2
FROM VALUES
(1234, 'OFFERhttps://www.google.com/'),
(3456, 'THEMEhttps://www.google.com/'),
(1235, 'OFFERhTTps://www.google.com/'),
(3455, 'THEMEhTTps://www.google.com/')
;
gives:
ID
WEB_NAME
SOLUTION_1
SOLUTION_2
1,234
OFFERhttps://www.google.com/
OFFER
OFFER
3,456
THEMEhttps://www.google.com/
THEME
THEME
1,235
OFFERhTTps://www.google.com/
OFFER
3,455
THEMEhTTps://www.google.com/
THEME
Which shows with any letter in the match not being the correct case, not match is made on the case sensitive match

Related

Querying a value from an XML in SQL Server 2017

I am struggling with querying a value from an XML. I am new in this world so please forgive me if the answer is actually obvious and i am just not seeing it :)
<documentid>
<id accountingentity="200" lid="...."
variationid="6370">aa0000012</id>
</documentid>
I want to query the value "aa0000012" but I am not able to do it since there is no Xpath for this value available.
I tried it with this query right here but it is only giving me the whole content:
select
#salesorderxml.query('/syncsalesorder/dataarea/salesorder/salesorderheader/documentid/id')
Can anyone please help me? Thanks
This should work whether you have multiple documentid's or not
select o.value('id[1]', 'varchar(max)')
from #salesorderxml.nodes('/documentid') as t(o)
it basically says give me the nodes named documentid, then give me the value of element id
Try this - it will reach into the XML and fetch the element value of <id>:
SELECT
XC.value('text()[1]', 'varchar(100)')
FROM
#salesorderxml.nodes('/documentid/id') AS XT(XC)
This returns:
(No column name)
----------------
aa0000012

SQL Server select looks like 'Like'

I have question about SQL Server's LIKE operator.
I have a table Table1:
app ex
----------
test 210
I am writing some select from program select is looks like this:
select app
from Table1
where ex Like = '210203'
I have tried to use (%,%.%,[]).
If I try [] this one like this [210]203 it is working but there will be more data so if there will be 2102 in ex I want this to choose this one.
But it isn't selecting nothing beacouse ver is 210203 and in ex is 210
How can I manage this that I selected 210 the variable can't be changed there will be always variable bigger than 'ex' data
Please help me anyone.
You query is not right, you don't use "=" operator in Like.
SELECT app FROM Table1 WHERE ex LIKE '%210%'
In this case will filter values who have 210 inside.
If you use for ex: Like ='%210' will filter values who ends with 210.. and so on.
Update
You can use this too, i think will help you
declare #Value varchar(50)
set #value = '123AAAAA123'
select * from Table where #Value like '%' + column + '%'

SQL select statement where the text I am looking for has single quotes

I am trying to do a select statement in Microsoft SQL server management studio. The data looks like this:
My select statement is:
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and TEXT = 'State changed from ''Awaiting Prep'' to ''Import Error'''
But this brings back no results, despite there being lots of records with this text.
I was able to retrieve some data by using this statement:
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and TEXT like '%Awaiting Prep%'
and TEXT like '%Import Error%'
but unfortunately it brings back too many results because it is bringing back data that states: 'State changed from 'Import Error' to 'Awaiting Prep''
where I am only looking for 'State changed from 'Awaiting Prep' to 'Import Error''
Please can anyone help. From reading other posts putting a double quote in should solve the issue of the single quotes but in this instance it doesn't work.
Many thanks
You can combine your LIKE to one using % as placeholders, instead of:
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and TEXT like '%Awaiting Prep%'
and TEXT like '%Import Error%'
do:
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and TEXT like '%Awaiting Prep%Import Error%'
You can try using LTRIM and RTRIM in sql server. If there is case of some white space. Otherwise your statement is seems ok
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and LTRIM(RTRIM(TEXT)) = 'State changed from ''Awaiting Prep'' to ''Import
Error'''

SQL - How can I return a value from a different table base on a parameter?

SQL - How can I return a value from a different table base on a parameter
First time poster, long time reader:
I am using a custom Excel function that allows be to pass parameters and build a SQL string that returns a value. This is working fine. However, I would like to choose among various tables based on the parameters that are passed.
At the moment I have two working functions with SQL statements look like this:
_______FUNCTION ONE________
<SQLText>
SELECT PRODDTA.TABLE1.T1DESC as DESCRIPTION
FROM PRODDTA.TABLE1
WHERE PRODDTA.TABLE1.T1KEY = '&PARM02'</SQLText>
_______FUNCTION TWO________
<SQLText>
SELECT PRODDTA.TABLE2.T2DESC as DESCRIPTION
FROM PRODDTA.TABLE2
WHERE PRODDTA.TABLE2.T2KEY = '&PARM02'</SQLText>
So I am using IF logic in Excel to check the first parameter and decide which function to use.
It would be much better if I could do a single SQL statement that could pick the right table based on the 1st parameter. Logically something like this:
_______FUNCTIONS COMBINED________
IF '&PARM02' = “A” THEN
SELECT PRODDTA.TABLE1.T1DESC as DESCRIPTION
FROM PRODDTA.TABLE1
WHERE PRODDTA.TABLE1.T1KEY = '&PARM02'
ELSE IF '&PARM02' = “B” THEN
SELECT PRODDTA.TABLE2.T2DESC as DESCRIPTION
FROM PRODDTA.TABLE2
WHERE PRODDTA.TABLE2.T2KEY = '&PARM02'
ELSE
DESCRIPTION = “”
Based on another post Querying different table based on a parameter I tried this exact syntax with no success
<SQLText>
IF'&PARM02'= "A"
BEGIN
SELECT PRODDTA.F0101.ABALPH as DESCRIPTION
FROM PRODDTA.F0101
WHERE PRODDTA.F0101.ABAN8 = '&PARM02'
END ELSE
BEGIN
SELECT PRODDTA.F4801.WADL01 as DESCRIPTION
FROM PRODDTA.F4801
WHERE PRODDTA.F4801.WADOCO = '&PARM02'
END</SQLText>
You could try using a JOIN statement.
http://www.sqlfiddle.com/#!9/23461d/1
Here is a fiddle showing two tables.
The following code snip will give you the values from both tables, using the Key as the matching logic.
SELECT Table1.description, Table1.key, Table2.description
from Table1
Join Table2 on Table1.key = Table2.key
Here's one way to do it. If PARM03='Use Table1' then the top half of the union will return records and vice versa. This won't necessarily product good performance though. You should consider why you are storing data in this way. It looks like you are partitioning data across different tables which is a bad idea.
SELECT PRODDTA.TABLE1.T1DESC as DESCRIPTION
FROM PRODDTA.TABLE1
WHERE PRODDTA.TABLE1.T1KEY = '&PARM02'
AND &PARM03='Use Table1'
UNION ALL
SELECT PRODDTA.TABLE2.T2DESC as DESCRIPTION
FROM PRODDTA.TABLE2
WHERE PRODDTA.TABLE2.T2KEY = '&PARM02'</SQLText>
AND &PARM03='Use Table2'

How to remove a full stop from the end of data

I need to update my table because a number of fields in a column have got a fullstop on the end, i need to remove this.
So, in TableA, Field1: the data looks like
1002243.
1007053.
1007403.
1104098.
1110010.
NOTE: Not all the fields are the same length.
I need to remove the full stops.
Im using SQL Server 2005, cheers :)
UPDATE TableA
SET Field1 = LEFT(Field1 ,LEN(Field1)-1)
WHERE Field1 LIKE '%.'
Then you have data after the period. Try this to see:
Print '[' + Field1 + ']' and see if you get something like this:
[101. ]
There is a space after the period.

Resources