CHARINDEX and SUBSTRING between two Pipes - sql-server-2008-r2

I have a string in a table :
S-1-5-21-109290937-1013972632-435976164-15678|l.smith|DOMAIN-UK|0x95231|1
I need to extract the data between the first and second | .
So from the above it would return l.smith only.
I have tried various CHARINDEX and SUBSTRINGS but it always errors, the length of the string also changes so I cant trim the other | out.

I tried this just now, Hope this is what you are looking for,
DECLARE #VALUE VARCHAR(MAX) = 'S-1-5-21-109290937-1013972632-435976164-15678|l.smith|DOMAIN-UK|0x95231|1';
DECLARE #FIRSTINDEX INT = CHARINDEX('|',#VALUE,1);
SELECT
SUBSTRING(#VALUE, #FIRSTINDEX+1, CHARINDEX('|',#VALUE,#FIRSTINDEX+1)-CHARINDEX('|',#VALUE,1)-1);

Related

Postgres: ANY function does not work on varchar array

I have a "product" table with a varchar[] column to keep excluded companies.
When I select the array for a specific product, I get it like this:
SELECT excluded_company_codes FROM product WHERE code = '123'
excluded_company_codes
----------
{'10'}
However, oddly enough, when I try to check if company code exists in the array with ANY function, it doesn't work:
SELECT '10'=ANY(product.excluded_company_codes) where code = '123'
?column?
----------
false
What am I doing wrong here?
The string in the array contains two single quotes. If you want to find that, you have to
SELECT '''10''' = ANY(product.excluded_company_codes)
FROM product
WHERE code = '123';
If you want to avoid doubling the quotes, you can use dollar quoting: $$'10'$$.

Extract specific words from text value

I have a query and a returned value that looks like this:
select properties->>'text' as snippet from table where id = 31;
snippet
-----------------------------------
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable.
(1 row)
This returns as I expect based on my query.
Is there a way that I can slice the returned text to only return words from position 5 to position 8 for example? Or alternatively, slice by character position which I will be able to use as a workaround?
I have tried using:
select properties->>'text'[0:13] as snippet from table where id = 31;
Which I hoped would return:
There are many
But it hasn't worked.
Is this possibly to slice a jsonb text field?
To "slice by character position", you can simply use the substr() function:
select substr(properties->>'text', 1, 15) as snippet
from the_table
where id = 31;
If you really want "words", you can split the text into an array using e.g. regexp_split_to_array. Once you have an array, you can use the slice syntax:
select (regexp_split_to_array(properties->>'text','\s+'))[5:8] as snippet
from the_table
where id = 31;
This returns an array, if you want it as a string, you can use array_to_string()
select array_to_string((regexp_split_to_array(properties->>'text','\s+'))[5:8],' ') as snippet
from the_table
where id = 31;
If you need that frequently, I would wrap it into a function:
create function extract_words(p_input text, p_start int, p_end int)
returns text
as
$$
select array_to_string((regexp_split_to_array(p_input,'\s+'))[p_start:p_end],' ');
$$
language sql
immutable;
Then the query is much easier to read:
select extract_words(properties->>'text', 5, 8) as snippet
from the_table
where id = 31;

T-SQL Substring of a Link

I have a string with which I want to retrieve a substring out of:
"app/reports/here"
The resulting substring I want is "app/reports"
I some T-SQL as below:
DECLARE #document varchar(64);
SELECT #document = 'app/reports/here';
SELECT substring(#document, 0, CHARINDEX('/', replace(#document,'app/','')));
GO
The result of the code above is "app/rep".
How could I get the full string I need? Someone about the CHARINDEX is confusing me..
Thanks
Just add 4 to the CHARINDEX since you replace 4 characters from the original string with an empty string so the result from CHARINDEX is shifted by that. So it should be:
DECLARE #document varchar(64);
SELECT #document = 'app/reports/here';
SELECT substring(#document, 0, CHARINDEX('/', replace(#document,'app/',''))+4);
GO

How to get last part of nvarchar with variable size in T-SQL?

Imagine that I have the following value in my nvarchar variable:
DECLARE #txt nvarchar(255)
SET #txt = '32|foo|foo2|123'
Is there a way to easily get the last part just after the last | that is 123 in this case ?
I could write a split function but I'm not interested in the first parts of this string. Is there another way to get that last part of the string without getting the first parts ?
Note that all parts of my string have variable sizes.
You can use a combination of LEFT, REVERSE and CHARINDEX for this.
The query below reverses the string, finds the first occurance of |, strips out other characters and then straightens the string back.
DECLARE #txt nvarchar(255)
SET #txt = '32|foo|foo2|123'
SELECT REVERSE(LEFT(REVERSE(#txt),CHARINDEX('|',REVERSE(#txt))-1))
Output
123
Edit
If your string only has 4 parts or less and . isn't a valid character, you can also use PARSENAME for this.
DECLARE #txt nvarchar(255)
SET #txt = '32|foo|foo2|123'
SELECT PARSENAME(REPLACE(#txt,'|','.'),1)
You can reverse your string to get the desired result:
DECLARE #txt nvarchar(255) = '32|foo|foo2|123'
SELECT REVERSE(SUBSTRING(REVERSE(#txt), 1, CHARINDEX('|', REVERSE(#txt)) -1))

how to get a substring from right with T-sql

Suppose I have a string like:
abc.efg.hijk.lmnop.leaf
I want the substring: abc.efg.hijk.lmnop.
Means: Find out the first comma . from right, then get the substring from left to this comma
How to use t-sql string function return the substring with one expresssion?
First your'll need to reverse the string and find the character index of the first period, then subtract this number from the length of the entire string. This value needs to be used at the length parameter of the sub-string function.
Try this:
DECLARE #S VARCHAR(55) = 'abc.efg.hijk.lmnop.leaf'
SELECT SUBSTRING(#S, 1, LEN(#S) - CHARINDEX('.', REVERSE(#S)))