how to use PARSENAME - sql

I get not the right result out of:
select parsename(replace('K.03.2_test', '_', '.' ), 2)
My result at the moment is:
2
...but I need all the string to the left from the underscore. So this is what I need:
K.03.2

To get the characters to the left of the underscore you can use
SELECT CASE
WHEN YourCol LIKE '%[_]%'
THEN LEFT(YourCol, CHARINDEX('_', YourCol) - 1)
END
FROM YourTable
parsename is designed to parse object identifiers not split arbitrary strings.

Related

SQL Select everything to the left of a character

I need to select everything to the left of a hyphen in a string, but some strings have 2 hyphens, in which case I need to select everything to the left of the second hyphen.
The strings are never the same length and some strings don't have hyphens at all.
Example data:
Manager-News Delivery
Co-Host-Television
Expected results:
Manager
Co-Host
How about just removing everything from the last hyphen onward?
You can do that with stuff():
select stuff(str, len(str) - charindex('-', reverse(str)) + 1, len(str), '')
from (values ('Co-Host-Television'), ('Manager-News Delivery')) v(str);
Or better yet, with left():
select left(str, len(str) - charindex('-', reverse(str)) )
from (values ('Co-Host-Television'), ('Manager-News Delivery')) v(str);
SQL isn't really meant for string manipulation, you should do that type of work in another language (on the client side? maybe).
But, it you really, really want to, and the data is small and not something you'll need to do a lot, you can use the following code:
select rtrim(reverse(substring(reverse('Manager-News Delivery'),
charindex('-',reverse('Manager-News Delivery'))+1,99)))
select rtrim(reverse(substring(reverse('Co-Host-Television'),
charindex('-',reverse('Co-Host-Television'))+1,99)))

TSQL extract part of string with regex

i would make a script that iterate over the records of a table with a cursor
and extract from a column value formatted like that "yyy://xx/bb/147011"
only the final number 147011and to put this value in a variable.
It's possible to do something like that?
Many thanks.
You don't need a cursor for this. You can just use a query. The following gets everything after the last /:
select right(str, charindex('/', reverse(str)) - 1 )
from (values ('yyy://xx/bb/147011')) v(str)
It does not specifically check if it is a number, but that can be added as well.
You can also use the below query.
SELECT RIGHT(RTRIM('yyy://xx/bb/147011'),
CHARINDEX('/', REVERSE('/' + RTRIM('yyy://xx/bb/147011'))) - 1) AS LastWord
If numeric value has exact position defined with sample data, then you can do :
SELECT t.*, SUBSTRING(t.col, PATINDEX('%[0-9]%', t.col), LEN(t.col))
FROM table t;

What is the best way to trim a string in T-SQL?

I want to trim a this character consist of a chinese character.
Select '10樓 10 /F'
Select '7樓 7/F'
Bad Result:
10樓 10 /F
7樓 7/F
I want to this:
10/F
7/F
You can use CHARINDEX to find the position of the chinese character.
Then use SUBSTRING to get the part you need based on that position.
And REPLACE all the spaces to nothing from that result.
For example:
select * , replace(substring(value, charindex(N'樓',value)+1, len(value)),' ','') as value2
from (values
(N'10樓 10 /F'),
(N'7樓 7/F')
) v(value);
Note that putting the N before the strings marks them as NVARCHAR.
Since the chinese character is a unicode character.
Try to do this:
with cte as (
select '10樓 10 /F' as t
union all
select '7樓 7/F'
)
select rtrim(ltrim(substring(t, charindex('樓', t) + 1, len(t)))) as t
from cte

Use of substring in SQL

My query is the following:
SELECT id, category FROM table1
This returns the following rows:
ID|category
1 |{IN, SP}
2 |
3 |{VO}
Does anyone know how i can remove the first char and last char of the string in PostgreSQL, so it removes: {}?
Not sure, what you mean with "foreign column", but as the column is an array, the best way to deal with that is to use array_to_string()
SELECT id, array_to_string(category, ',') as category
FROM table1;
The curly braces are not part of the stored value. This is just the string representation of an array that is used to display it.
Either using multiple REPLACE functions.
SELECT id, REPLACE(REPLACE(category, '{', ''), '}', '')
FROM table1
Or using a combination of the SUBSTRING, LEFT & LENGTH functions
SELECT id, LEFT(SUBSTRING(category, 2, 999),LENGTH(SUBSTRING(category, 2, 999)) - 1)
FROM table1
Or just SUBSTRING and LENGTH
SELECT id, SUBSTRING(category, 2, LENGTH(category)-2)
FROM table1
You could replace the {} with an empty string
SELECT id, replace(replace(category, '{', ''), '}', '') FROM table1
select id,
substring(category,charindex('{',category,len(category))+2,len(category)-2)
from table1;
select id
,left(right(category,length(category)-1),length(category)-2) category
from boo
select id
,trim(both '{}' from category)
from boo
Trim():
Remove the longest string containing only the characters (a space by
default) from the start/end/both ends of the string
The syntax for the replace function in PostgreSQL is:
replace( string, from_substring, to_substring )
Parameters or Arguments
string
The source string.
from_substring
The substring to find. All occurrences of from_substring found within string are replaced with to_substring.
to_substring
The replacement substring. All occurrences of from_substring found within string are replaced with to_substring.
UPDATE dbo.table1
SET category = REPLACE(category, '{', '')
WHERE ID <=3
UPDATE dbo.table1
SET category = REPLACE(category, '}', '')
WHERE ID <=3

SQL Replace command with wildcards

Basically I'm trying to replace everything in a string that is within brackets.
So for example the string of '123[1abc]abc' would become '123[xx]abc'
select replace(string,'[%]','[xx]') as string2 from table1
Except of course that won't work.
The value within the brackets is always different, and it is simply not feasible to find all the individual possibilities. In addition, some of the values within the brackets also appear outside of the brackets, but I only want them changed for the part within.
I'm working in Microsoft SQL Server if that makes any difference.
Assuming there is only one such expression and the square braces only appear once, you can use stuff() to construct the string:
select stuff(str,
charindex('[', str) + 1,
charindex(']', str) - charindex('[', str) - 1,
'xx')
You could do it with string manipulation. Take the left of the string up to the left bracket, add 'xx' and then add the right side starting with the right bracket.
SELECT LEFT(string, CHARINDEX('[', string))
+ 'xx'
+ RIGHT(string, LEN(string) - CHARINDEX(']', string) + 1) AS string2
FROM table1;