How to use tSQL to match and remove everything after either of 2 strings? - tsql

How can I use tSQL to find one of two strings, and if they exist, return everything before found string?
In an ETL process, how would we take the column from source, identify the strings ?uniquecode= OR /uniquecode= and therefore remove those, and everything else after them, in the SELECT statement for the sink column? i.e. matching desired outcome below.
On this SO question I was provided with a solution that finds ?uniquecode= successfully. I just need to find a way to modify it to also look for /uniquecode=
SELECT
CASE WHEN CHARINDEX('?uniquecode=', SourcePageURL) > 0
THEN SUBSTRING(SourcePageURL,
1,
CHARINDEX('?uniquecode=', SourcePageURL) - 1)
ELSE SourcePageURL END AS new_source
FROM sql_test;

You may modify your current query as follows:
SELECT
CASE WHEN SourcePageURL LIKE '%?uniquecode=%'
THEN SUBSTRING(SourcePageURL,
1,
CHARINDEX('?uniquecode=', SourcePageURL) - 1)
WHEN SourcePageURL LIKE '%/uniquecode%'
THEN SUBSTRING(SourcePageURL,
1,
CHARINDEX('uniquecode=', SourcePageURL) - 1)
ELSE SourcePageURL END AS new_source
FROM sql_test;
Demo

Related

REDSHIFT if value in list

I am trying to set some variables on the top of my query via CTEs to make maintenance of a long query more easy to handle.
I have extracted an example of what I am trying to achieve. I am not managing to make 'tags' be perceived as a list rather than a whole string. I have tried split_part but have not managed to get what I require.
WITH tmp AS (
SELECT
'tag1, tag2, tag3' as tags
)
select
CASE WHEN 'tag1' in (select tags from tmp) THEN 1 ELSE 0 END matched_tags
Basically what I need is to have a string 'tag1' and see if it exists in the list 'tag1','tag2' or 'tag3'. This should give me 1 as there is a match
This is obviously not working because it is taking the 'tag1, tag2, tag3' as one string so there is no match.
Can anyone help me with this?
The STRPOS() function should do what you want. https://docs.aws.amazon.com/redshift/latest/dg/r_STRPOS.html
Something like this:
WITH tmp AS (
SELECT
'tag1, tag2, tag3' as tags
)
SELECT
CASE WHEN STRPOS(tags, 'tag1') > 0 THEN 1 ELSE 0 END as matched_tags
FROM tmp;

How to use tSQL to match and remove everything after a string?

How can I use tSQL to find a string, and if it exists, return everything before that string?
i.e. in the example below, in an ETL process, how would we take the column from source, identify the string ?uniquecode= and therefore remove that, and everything else after it, in the SELECT statement for the sink column?
How can I best modify this tSQL statement below to return the values in SinkPageURL column above?
SELECT SourcePageURL FROM ExampleTable
I have attempted a Fiddle here - http://sqlfiddle.com/#!18/3b60a/4 using the below statement. It is disregarding the values where '?uniquecode=' does not exist though, and also leaves the '?' symbol. Need this to work with MS SQL Server '17.
Somewhat close, but no cigar. Help appreciated!
SELECT LEFT(SourcePageURL, CHARINDEX('?uniquecode=', SourcePageURL)) FROM sql_test
Try this query:
SELECT
CASE WHEN CHARINDEX('?uniquecode=', SourcePageURL) > 0
THEN SUBSTRING(SourcePageURL,
1,
CHARINDEX('?uniquecode=', SourcePageURL) - 1)
ELSE SourcePageURL END AS new_source
FROM sql_test;
If you instead wanted to update the source URLs in your example using this logic, you could try the following:
UPDATE sql_test
SET SourcePageURL = SUBSTRING(SourcePageURL,
1,
CHARINDEX('?uniquecode=', SourcePageURL) - 1)
WHERE SourcePageURL LIKE '%?uniquecode=%';

Can you do a sub select within a Case statement

Probably something really trivial but I haven't quite found the answer I am looking for on the internet and I get syntax errors with this. What I want/need to do is to provide a special case in my where clause where the doctype is 1. If it is, then it needs to match the claimID from a sub select of a temp table. If the doctype is not a 1 then we just need to continue on and ignore the select.
AND
CASE
WHEN #DocType = 1 THEN (c.ClaimID IN (SELECT TNE.ClaimID FROM TNE)
END
I have seen some for if statements but I didn't seem to get that to work and haven't found anything online as of yet that shows a case statement doing what I would like. Is this even possible?
You don't need a case statement, you could do:
AND (#DocType <> 1 or c.ClaimID in (SELECT TNE.ClaimID FROM TNE))
A CASE expression (not statement) returns a single value. SQL Server supports the bit data type. (Valid values are 0, 1, 'TRUE' and 'FALSE'.) There is a boolean data type (with values TRUE, FALSE and UNKNOWN), but you cannot get a firm grip on one. Your CASE expression attempts to return a boolean, give or take the unmatched parenthesis, which is not supported in this context.
You could use something like this, though Luc's answer is more applicable to the stated problem:
and
case
when #DocType = 1 and c.ClaimId in ( select TNE.ClaimId from TNE ) then 1
when #DocType = 2 and ... then 1
...
else 0
end = 1
Note that the CASE returns a value which you must then compare (= 1).

How to find a substring and delete it as well as everything that comes after it

I've been searching for a way to update some columns of a certain table.
This update would try to find a certain substring and to delete it as well as every other characters that comes after it.
It's easy to delete everyhting after a certain character but I can't find a way to do the same thing with a substring.
Thanks for the help
Considering that:
the column to update is called haystack
the substring to search for is called #needle
Here's the expression you're looking for:
case
when charindex(#needle, haystack) = 0 then #haystack
else substring(haystack, 1, charindex(#needle, haystack) - 1)
end
Here's an online test of the two cases (match/no match): http://www.sqlfiddle.com/#!3/d41d8/21209
DECLARE
#substring varchar(32) = 'anySubString'
UPDATE
myTable
SET
colomnName =
(CASE WHEN charindex(#substring, colomnName) = 0 THEN
colomnName
ELSE
substring(colomnName , 1, charindex(#substring, colomnName) - 1)
END)
END

Remove last character from string column in SQL Server CE

In SQL Server Compact, I'm trying to remove a trailing comma that came from a goof which affected several thousand rows of a NVARCHAR column.
UPDATE myTable
SET col = LEFT(col, LEN(col)-1)
WHERE col LIKE '%,';
throws the error:
There was an error parsing the query. [ Token in error = LEFT ]
Can SQL Server CE not parse that query? Or, can someone offer another approach?
Note: I tried this in CompactView, I'm not sure if that's the problem.
Based off this example I was able to get it done using SUBSTRING:
UPDATE myTable
SET col = SUBSTRING(col, 0, LEN(col))
WHERE col LIKE '%,';
The proposed solution with using SET col = SUBSTRING(col, 0, LEN(col)) is a bit unclear.
This is working as a side effect of the SUBSTRING second parameter starting_position being "1 based". So 0 in this case is kind of negative (you could also use i.e. -3 and 4 characters would be stripped then instead of 1).
IMHO it would be much more clear to use this:
UPDATE myTable
SET col = SUBSTRING(col, 1, LEN(col)-1)
WHERE col LIKE '%,';
Which shows the code's intent
UPDATE myTable
SET col = SUBSTR(col, 0, (LENGTH(col) - 1))
WHERE col LIKE '%,';