Extract only numeric values from String column - sql

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)

Related

Substring of a specific occurence

I have a column as varchar2 datatype, the data in it is in format:
100323.3819823.222
100.323123.443422
1001010100.233888
LOL12333.DDD33.44
I need to remove the whole part after the first occurrence of '.'
In the end it should look like this:
100323
100
1001010100
LOL12333
I cant seem to find the exact substring expression due to the fact that there is not any fix length of the first part.
One way is to use REGEXP_SUBSTR:
SELECT REGEXP_SUBSTR(column_name,'^[^.]*') FROM table
The other way is to combine SUBSTR with INSTR, which is a bit faster, but will result in NULL if the data doesn't contain a dot, so you'll have to add a switch if needed:
SELECT SUBSTR(column_name, 1, INSTR(column_name,'.') - 1) FROM table
For oracle you can try this:
select substr (i,1,Instr(i,'.',i)-1) from Table name.

pull characters from string based on fixed character

In an SQL query, I know how to use Left, Right and Mid but is there a way to pull values from a column leading up to a specific character in the column? Such as a column that looks like this:
testemail#test.com|Something Else|Error
I want everything from the left up to the first | but since this is an email address, there isn't a fixed value of characters in the address. I think this is easy but I just can't remember the function for this if there is one.
Thanks.
Try using this with substring and charindex:
Select substring([field],1,charindex('|',[field1]) -1 ) from table1
Replace field1 with your field.
CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] )
Searches an expression for another expression and returns its starting position if found.

how to print some letters from a string in sql server

I had a table like bellow image. I want 2,3 letters from the column "name"
The result is like bc,da,bc,bc,bc,bc
SELECT SUBSTRING(name,2,2) AS ShortComp FROM table;
You can also use the string function LEFT() to return the leftmost specified number of characters, like this:
SELECT LEFT(name, 2) AS ShortComp FROM table;

substring varchar get first globalid

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

Searching set of characters in a string

How to query if i want to check if string contains numbers(characters) like 1,2,3,4,5,6,7 in any order. It can be achieved by writing AND statements on LIKE clause for number 1,2..and so on like below,
Where days like '%1%' and days like '%2%' ...... So on
Is there any query which check specific characters present in string. or how above example can achieve with a short hand query. Please help. Thanks.
One way to do this is create a table with all the string you like to search.
e.g.
DECLARE #searchstr TABLE (s VARCHAR(10))
INSERT INTO #searchstr VALUES ('1'),('2'),('3'),('4'),('5'),('6'),('7')
DECLARE #tbl TABLE (days VARCHAR(100))
INSERT INTO #tbl VALUES ('1234567'),('123'),('1122334'),('7654321')
SELECT t.days
FROM #tbl t
LEFT JOIN #searchstr s
ON t.days LIKE '%' + s.s+ '%'
GROUP BY t.days HAVING COUNT(DISTINCT s.s) = 7
Will not CONTAINS() work for you?
Also notice how to split word into char array.
I would suggest looking into [PATINDEX][1]. From MSDN:
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.
You probably will end up using something like WHERE PATINDEX(%[0-9]%', foo) > 0
You can use CHARINDEX
CHARINDEX(stringthatcontainschars1, '1', numofpositiontostartlookingat)
Output: 24, the position that your searched for char is at. returns zero if not found
if you were looking for the number 1 in a column or a string you would put:
CHARINDEX(columnname, '1', 1)
This would search for '1' beginning at position 1.
You can do an conditional statement based on whether or not it returns 0 to decide if char is in string or not and write whatever code you need after that.
I'd use isnumeric to ensure they're all digits, then test for 0, 8, or 9 instead of 1-7 because it's shorter.
If days are coma separated you can use FIND_IN_SET
SELECT FIND_IN_SET(1, days) AND FIND_IN_SET(2, days);
It is different, not better, but as far as I know there is no other native way.
And LIKEs probably will be much faster then find_in_set