substring varchar get first globalid - sql

I have varchar string and need return first globalid value - 8679926300927194610
My string:
declare #erservice varchar(max) = 'globalid=8679926300927194610,ou=services,globalid=00000000000000000000'

You can do with the following. Assuming that the string always starts globalid=, and there is a comma after the number.
SELECT SUBSTRING(#erservice,10,CHARINDEX(',',#erservice)-10)

You could use substring and charindex, given that the string always starts with globalid=n
For easier readability I will replace the string with #yText
(#yText = 'globalid=8679926300927194610,ou=services,globalid=00000000000000000000')
DECLARE #erservice varchar(max) = substring(#yText, 10, CHARINDEX(',',#yText)-10)
Substring will give you the offset to start extracting from, while Charindex will show the end of the numbers by searching for the place of the comma.
http://social.technet.microsoft.com/wiki/contents/articles/17948.t-sql-right-left-substring-and-charindex-functions.aspx

Related

Extract only numeric values from String column

I have a column of values that are as shown below:
ID
x-644478134
x-439220334
x-645948923
x-10686843432
x-4273883234
I would like to return a column like so:
ID
644478134
439220334
645948923
10686843432
4273883234
Can someone advise how to do this cleanly? I believe it is something to do with substring but not sure exactly
SELECT SUBSTRING(d.ID)
FROM data d
You need to use substring, charindex and len functions such as below, assuming the dash (-) is the separator of the text and numeric part:
declare #x varchar(50) = 'a-0123'
select substring(#x,CHARINDEX('-',#x,1)+1,len(#x)-CHARINDEX('-',#x,1))
If you are sure that ID always starts with x-, then:
Select substring(ID,3,LEN(ID)-2)

How to remove digits and special characters from the beginning of a string?

For instance I have
'234 - ? Hi there'
The result should be:
'Hi there'
For oracle you have the regexp_replace function. So you could do the below to replace non-alphabetic characters from the beginning of the string:
select regexp_replace('24 Hi','^([^a-zA-Z]*)','') from dual
The first ^ in ^([^a-zA-Z]*) is to match the beginning of the string. The second ^ is to match any non-alphabetic characters.
In Oracle you can use REGEXP_REPLACE(). I recommend using a slightly different regex than the one in the accepted answer; there's no reason to do any replacing on a pattern that can be of zero width. Additionally, the parentheses are unnecessary since you don't need to capture a group:
SELECT REGEXP_REPLACE(my_column, '^[^A-Za-z]+') FROM my_table;
We can also exclude the 3rd argument to REGEXP_REPLACE since in Oracle, a NULL and an empty string are equivalent. Another alternative in Oracle is to use the POSIX character class [:alpha:]:
SELECT REGEXP_REPLACE(my_column, '^[^[:alpha:]]+')
FROM my_table;
Please see the SQL Fiddle here. You can read more about POSIX character classes here.
Use this Function to remove numeric and Special symbols.
CREATE function [dbo].[RemoveNumericandSpecialSymbolValue](#str varchar(500))
returns varchar(500)
begin
declare #text int
set #text=0
while 1=1
begin
set #text= patindex('%[^a-z .]%',#str)
if #text <> 0
begin
set #str = replace(#str,substring(#str,#text,1),'')
end
else break;
end
return #str
end
Example:
select dbo.RemoveNumericandSpecialSymbolValue('234 - ? Hi there')

SQL Server substring without upper bound

What is the best way in SQL Server to do
SELECT
SUBSTRING('CATCH ME IF YOU CAN', 2, 10000)
-- ATCH ME IF YOU CAN
with no upper bound?
Use STUFF instead (STUFF (Transact-SQL)):
SELECT STUFF('CATCH ME IF YOU CAN',1,1,'');
Here, STUFF replaces 1 character, from position 1 with the string ''. The 2nd parameter is the start position, and the 3rd is the number of characters (from that position) to replace. The 4th is the replacement string.
So, as a further example, you could do something like this:
SELECT STUFF('2019-07-09 11:38:00',11,1,'T');
This replaces 1 character from position 11 with the character 'T', which returns '2019-07-09T11:38:00', changing the above value to the ISO8601 format. As you can see, the length of the string to replace does not need to be the same length as the replacement string as well (in fact, the 3rd parameter can have a value of 0, meaning that no characters are replaced and the "replacement" string is simply injected into the existing value).
Using LEN() with variable
DECLARE #Val AS VARCHAR (MAX) = 'CATCH ME IF YOU CAN';
SELECT SUBSTRING(#Val, 2, LEN(#Val));
or directly with LEN()
SELECT SUBSTRING('CATCH ME IF YOU CAN', 2, LEN('CATCH ME IF YOU CAN'));
You can use RIGHT() function, combined with LEN().
All you have to do is subtract from LEN() the number of chars that you want to exclude from the start of the string:
SELECT RIGHT('CATCH ME IF YOU CAN', LEN('CATCH ME IF YOU CAN') - 1)

Remove last x characters until a specific character

I got this string /uk-en/contact-us/frequently-asked-questions/your-trip/there-wi-fi-access-in-the-eurostar-terminals-and-board-your-trains and I need to get just the last part of the URL (until the last /).
Then I want to replace '-' with a space. The strings are not with the same number of characters.
How can I do?
Thank you!
Solution using BigQuery functions:
select regexp_replace(last(split(x, "/")), "-", " ") from
(select
"/uk-en/contact-us/frequently-asked-questions/your-trip/there-wi-fi-access-in-the-eurostar-terminals-and-board-your-trains"
as x)
Here is what I tried in SQL Server
DECLARE #s VARCHAR(max)= '/uk-en/contact-us/frequently-asked-questions/your-trip/there-wi-fi-access-in-the-eurostar-terminals-and-board-your-trains'
SELECT REVERSE(SUBSTRING(REVERSE(#s),CHARINDEX('/',REVERSE(#s)),LEN(REVERSE(#s))))+REVERSE(REPLACE(SUBSTRING(REVERSE(#s),1,CHARINDEX('/',REVERSE(#s))-1),'-',' '))
Sorry this was for SQL Server
did you try using split in big query
SPLIT('str' [, 'delimiter']) Returns a set of substrings as a repeated string. If delimiter is specified, the SPLIT function breaks str into substrings, using delimiter as the delimiter.

Transact SQL replace part of string

Is it possible to delete part of string using regexp (or something else, may be something like CHARINDEX could help) in SQL query?
I use MS SQL Server (2008 most likely).
Example: I have strings like "[some useless info] Useful part of string" I want to delete parts with text in brackets if they are in line.
Use REPLACE
for example :
UPDATE authors SET city = replace(city, 'To Remove', 'With BLACK or Whatever')
WHERE city LIKE 'Salt%'; // with where condition
You can use the PATINDEX function. Its not a complete regular expression implementation but you can use it for simple things.
PATINDEX (Transact-SQL)> Returns the starting position of the first occurrence of a pattern in a specified expression, or zeros if the pattern is not found, on all valid text and character data types.
OR You can use CLR to extend the SQL Server with a complete regular expression implementation.
SQL Server 2005: CLR Integration
SELECT * FROM temp where replace(replace(replace(url,'http://',''),'www.',''),'https://','')='"+url+"';
You can use STUFF to insert a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.
For example, the code below, replaces the 5 with 666666:
DECLARE #Variable NVARCHAR(MAX) = '12345678910'
SELECT STUFF(#Variable, 5, 1, '666666')
Note, that the second argument is not a string, it is a position and you are able to calculate it position using CHARINDEX for example.
Here is your case:
DECLARE #Variable NVARCHAR(MAX) = '[some useless info] Useful part of string'
SELECT STUFF(
#Variable
,CHARINDEX('[', #Variable)
,LEN(SUBSTRING(#Variable, CHARINDEX('[', #Variable), CHARINDEX(']', #Variable) - LEN(SUBSTRING(#Variable, 0, CHARINDEX('[', #Variable)))))
,''
)
Finally helps REPLACE, SUBSTRING and PATINDEX.
REPLACE(t.badString, Substring(t.badString , Patindex('%[%' , t.badString)+1 , Patindex('%]%' , t.badString)), '').
Thanks to all.