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;
Related
I’m trying to dynamically extract a substring from a very long URL. For example, I may have the following URLs:
https://www.google.com/ABCDEF Version=“0.0.00.0” GHIJK
https://www.google.com/ABCDEFGH Version=“0.0.0.0” IJKLM
https://www.google.com/ABC Version=“0.0.0.00” 12345
I am trying to extract the version code only (0.0.0.0).
This is what I have so far:
SELECT SUBSTR(col, INSTR(col, ‘Version=“‘)+9)
FROM table
This query returns the following result:
0.0.00.0” GHIJK … (url continues on)
So, I attempt to find “Version” in the link, so I can start from the same position in each row. This works fine, however I’m having a hard time dynamically locating the ending quote (“). I tried using INSTR in the third parameter of my SUBSTR function, like so:
SELECT SUBSTR(col, INSTR(col, ‘Version=“‘)+9, INSTR(col, ‘“‘))
FROM table
I figured that this would find the position of the ending quote, and then use that number for the length, but it returns a strange output. I’ve also used POSITION, CHARINDEX, LENGTH, and LOCATE. None of these functions work in Oracle.
I think maybe when I put +9 after the first INSTR function, it’s setting the query to a fixed position instead of a dynamic one, but I’m not sure how else to remove ‘Version=“‘.
Here's one option (which, actually, selects what's between double quotes - that's version in your example; if there were some other similar substring, you'd get a wrong result).
with test (col) as
(select 'https://www.google.com/ABCDEF Version="0.0.00.0" GHIJK' from dual union all
select 'https://www.google.com/ABCDEFGH Version="0.0.0.0" IJKLM' from dual union all
select 'https://www.google.com/ABC Version="0.0.0.00" 12345' from dual
)
select col,
replace(regexp_substr(col, '".+"'), '"') version
from test;
which results in
https://www.google.com/ABCDEF Version="0.0.00.0" GHIJK 0.0.00.0
https://www.google.com/ABCDEFGH Version="0.0.0.0" IJKLM 0.0.0.0
https://www.google.com/ABC Version="0.0.0.00" 12345 0.0.0.00
You can still use use INSTR to locate the second " in the string, then subtract the location of the first " to get the length that you need to get. Below is an example query:
SELECT col,
SUBSTR (col, INSTR (col, '"') + 1, INSTR (col, '"', 1, 2) - INSTR (col, '"') - 1) version
FROM test;
You can use REGEXP_SUBSTR() with Version=(\d.*\d?) pattern in order to extract the piece between Version=" and "(your quotes are presumed to be regular double quotes " ")
SELECT REGEXP_SUBSTR(url,'Version="(\d.*\d)"',1,1,null,1) AS version
FROM t
where
the third argument(1) is position,
the fourth argument(1) is occurence, and especially important to use the last one as being capture group (1)
indeed using '"(\d.*\d)"' pattern is enough for the
current data set
or
REGEXP_REPLACE() with capture group \2 as
SELECT REGEXP_REPLACE(url,'^(.*Version=")([^"]*).*','\2') AS version
FROM t
Demo
How can I find strings in a column that are doubled-up and correct them? I feel like there is an easy answer to this I just can't think of it.
Example:
I want to find instances of a repeating string, example "SolonSolon", and then update the column to "Solon".
Update:
They're always the same. No extra characters, but might have a space as part of the repeating value. Other examples would be...
"PlacePlace", "TreeTree", "OrangeOrange", "TravisMemorialHSTravisMemorialHS", "Texas HSTexas HS"
You can check if the string is equal to the first half replicated.
SELECT LEFT(YourCol,LEN(REPLACE(YourCol, ' ', 'x'))/2)
FROM YourTable
WHERE YourCol = REPLICATE(LEFT(YourCol,LEN(REPLACE(YourCol, ' ', 'x'))/2),2)
The reason for the REPLACE of spaces with x before calculating the LEN is because trailing spaces are ignored by this function. You can also use the technique in #lptr's answer for this but an edge case will be if the string was varchar(8000) and already 8000 characters long in which case concatenating an extra character won't do anything (LEN(SPACE(8000) + 'x') is 0).
..replace the first half of the value with an empty string..if there is nothing left..the value consists of two equal parts
select *, substring(c, 1, (len(c+'.')-1)/2)
from
(
values
('solosolo'), ('yoyo'), ('andand'), ('1212'),(' . .'),
('ababc'), ('onetwoone')
) as t(c)
where replace(c, substring(c, 1, (len(c+'.')-1)/2), '') = '';
Another alternative. The query removes inner spaces using REPLACE(str_col, ' ', ''), removes leading/traling spaces using TRIM, and checks to make sure the first half of the string equals the second half.
select left(no_spaces.str_col, v.str_len/2)
from foo f
cross apply (values (replaced trim(f.str_col), ' ', '')) no_spaces(str_col)
cross apply (values (len(no_spaces.str_col))) v(str_len)
where no_spaces.str_col=replicate(left(f.str_col, v.str_len/2), 2);
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)))
I'm using SQL Server 2008 and I'm trying to trim values that looks like this
DocID
----------------
FOO_1_23_456
FOO1_1_23_4567
I'm trying to make it so it will only give me everything after the first '_'
Result
_1_23_456
_1_23_4567
Right now my query is
select
right(DocIDDocument, LEN(DocID.Document) - 3)) AS NewDocID
which only the trims the first 3 characters, I need it to where it trims everything before the first '_'
Thanks
Use stuff() and charindex():
select stuff(document, 1, charindex('_', document) - 1, '')
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.