Select only characters in SQL - sql

I have strings in a database like this:
firstname.lastname#email.com
And I only need the characters that appear after the # symbol and before (.) symbol i.e. (email) from the above example
I am trying to find a simple way to do this in SQL.

Do this:
use [your_db_name];
go
create table dbo.test
(
string varchar(max) null
)
insert into dbo.test values ('firstname.lastname#email.com')
select
string,
substring(
string,
charindex('#', string, 0) + 1,
charindex('.', string, charindex('#', string, 0)) - charindex('#', string, 0) - 1
) as you_need
from dbo.test

String manipulations are such a pain in SQL Server. Here is one method:
select t.*,
left(en.emailname, charindex('.', en.emailname + '.') - 1)
from t outer apply
(select stuff(email, 1, charindex('#', email + '#'), '') as emailname) en;
That that in the charindex() calls, the character being searched for is placed at the end of the string. This allows the code to work even for malformed emails -- it returns an empty string when the email is not of the form '%#%.%'.

DECLARE #col char(200)
set #col = 'firstname.lastname#email.com'
SELECT SUBSTRING(#col, LEN(LEFT(#col, CHARINDEX ('#', #col))) + 1, LEN(#col) - LEN(LEFT(#col, CHARINDEX ('#', #col))) - LEN(RIGHT(#col, LEN(#col) - CHARINDEX ('.', #col))) - 4);

DECLARE #str varchar(50) = 'firstname.lastname#email.com';
SELECT LEFT(
RIGHT(#str, LEN(#str) - CHARINDEX('#', #str))
,CHARINDEX('.', RIGHT(#str, LEN(#str) - CHARINDEX('#', #str))
) - 1) AS OUTPUT
Above query gives only domain-name from Email. The query can be applied for column in a table

Try This:-
DECLARE #Text varchar(100)
SET #Text = 'firstname.lastname#email.com'
SELECT SUBSTRING(STUFF(#Text, 1, CHARINDEX('#',#Text), ''), 0,
CHARINDEX('.', STUFF(#Text, 1, CHARINDEX('#',#Text), '')))
Result:-
email

DECLARE #myStr varchar(100) = 'firstname.lastname#email.com'
SELECT
SUBSTRING(SUBSTRING(#myStr,CHARINDEX('#',#myStr)+1,LEN(#myStr)-CHARINDEX('#',#myStr)+1),0,CHARINDEX('.',SUBSTRING(#myStr,CHARINDEX('#',#myStr)+1,LEN(#myStr)-CHARINDEX('#',#myStr)+1)))
That can be useful but I really recommend you to build user defined function in C#/Visaul basic they could be much more faster that this.

Using charindex, len and reverse to search for the positions of the # and the last dot.
And substring to get the name based on those positions:
create table test (id int identity(1,1), email varchar(60));
insert into test (email) values
('jane.doe#email.com'),
('not an email'),
('#invalid.email.xxx'),
('john.doe#longer.domainname.net');
select *,
(case
when email like '[a-z]%#%.%'
then substring(email,
charindex('#',email)+1,
len(email) - charindex('#',email) - charindex('.',reverse(email))
)
end) as email_domain_without_extension
from test;
The CASE WHEN is used to return NULL when it's not an email (instead of an empty string).

Related

SQL Server remove part of string between characters

I have emails that look like this:
john.doe.946a9979-2951-4852-9e79-ad03eb0c1e5d#gmail.com
I am trying to get this output:
john.doe#gmail.com
I have this so far.... it's close.
SELECT
Caller = REPLACE(Caller,
SUBSTRING(Caller,
CHARINDEX('.', Caller),
CASE WHEN CHARINDEX('#', Caller, CHARINDEX('.', Caller)) > 1 THEN
CHARINDEX('#', Caller, CHARINDEX('.', Caller)) - CHARINDEX('.', Caller)
ELSE
LEN(Caller)
END ) , '')
FROM
some.table
Hmmm. I suspect the string you want to remove is fixed in length. So how about:
select stuff(caller, charindex('#',caller ) - 37, 37, '')
If you have SS 2016 or later, you can use R code to do it - granted I don't know about speed on larger data so be wary if its a production environment. Also be warned Regexp isn't my strongest area so you may want to check that portion.
DECLARE #dummyScript NVARCHAR(1000) = '
SELECT * FROM
(VALUES (''john.doe.946a9979-2951-4852-9e79-ad03eb0c1e5d#gmail.com''),
(''jane whoever 1234453-534343#yahoo.com'') ) t (Email)
'
DECLARE #myRcode NVARCHAR(600)
SET #myRcode = 'OutputDataset <- data.frame(Email_Cleaned = gsub("[0-9]+.+#", "#", InputDataSet$Email)) '
DECLARE #CleanedTable TABLE (Email_Cleaned VARCHAR(500))
INSERT INTO #CleanedTable
EXEC sp_execute_external_script
#language = N'R'
,#script = #myRcode
,#input_data_1 = #dummyScript
,#input_data_1_name = N'InputDataSet'
,#output_data_1_name = N'OutputDataset'
SELECT * FROM #CleanedTable
Here is a simpler method using LEFT(), RIGHT(), and CHARINDEX() functions
DECLARE #Caller VARCHAR(MAX) = 'john.doe.946a9979-2951-4852-9e79-ad03eb0c1e5d#gmail.com'
SELECT
LEFT(#Caller, CHARINDEX('.', #Caller, CHARINDEX('.', #Caller) + 1) - 1) + RIGHT(#Caller, LEN(#Caller) - CHARINDEX('#', #Caller) + 1) Email
The left side will get all the characters until the second dot, and the right side will get the characters from # sign to the end of characters.
Try like below
SELECT
REPLACE(CALLER,
Substring(CALLER,
PATINDEX('.[0-9]%#', CALLER),
PATINDEX('#', CALLER ) )
,'#')
From Table

Return a SUBSTRING with a known Prefix and Suffix Character

I have a table that has a Prefix of variable length that always ends with . and a suffix of variable length that always begins with -. The - can also be found in the string of text that I am looking for, so using REVERSE() is likely the best way to identify the suffix -.
ex: Prefix1.ABC-123-Suffix1
I'd like the SUBSTRING() result to look like: ABC-123
I was able to create both a SUBSTRING() to remove the Prefix and a SUBSTRING() to remove the suffix, but I'm having a hard time applying both. The returned text can be variable length.
I'm trying to avoid declaring a variable, but am open to it. I'd prefer to only use SUBSTRING(), REVERSE(), and CHARINDEX() if possible.
This is what I have so far:
SELECT [Col1]
,SUBSTRING((Col1),CHARINDEX('.', (Col1)) + 1, 999) AS [StrippedPrefix]
,REVERSE(SUBSTRING(REVERSE(Col1),CHARINDEX('-', REVERSE(Col1)) + 1, 999)) AS [StrippedSuffix]
--new SUBSTRING() with both Prefix and Suffix stripped
FROM [Table1]
Declare #YourTable table (Col1 varchar(50))
Insert Into #YourTable values
('Prefix1.ABC-123-Suffix1')
Select *
,String=Substring(Col1,charindex('.',Col1)+1,len(Col1)-charindex('.',Col1)-charindex('-',reverse(Col1)))
From #YourTable
Returns
Col1 String
Prefix1.ABC-123-Suffix1 ABC-123
As per M Ali's suggestion (Even better)
Select *
,String=PARSENAME(REPLACE(Col1 , '-', '.') , 3) + '-' + PARSENAME(REPLACE(Col1 , '-', '.') , 2)
From #YourTable
Here the varaiable is only used as demo - should work with your table as well:
DECLARE #s nvarchar(100) = 'Prefix1.ABC-123-Suffix1'
SELECT #s,
SUBSTRING(#s, 1, CHARINDEX('.', #s)-1) StrippedPrefix,
SUBSTRING(#s, LEN(#s)-CHARINDEX('-', REVERSE(#s))+2, CHARINDEX('-', REVERSE(#s))) StrippedSuffix,
SUBSTRING(#s, CHARINDEX('.', #s)+1, LEN(#s)-CHARINDEX('.', #s)-CHARINDEX('-', REVERSE(#s))) NewString
Here is one way
DECLARE #str VARCHAR(100)= 'Prefix1.ABC-123-Suffix'
SELECT Reverse(Stuff(intr_str, 1, Charindex('-', intr_str), ''))
FROM (SELECT Reverse(Stuff(#str, 1, Charindex('.', #str), '')) AS intr_str) a
Result : ABC-123

Return a substring from a specified string in SQL Server

I've below query:
DECLARE #url varchar (max)='http://v.mercola.com/blogs/public_blog/New-Diet-Pill-Expands-1-000-Times-in-Your-Stomach-24728.aspx'
SELECT replace(replace(RIGHT(#URL , CHARINDEX ('/' ,REVERSE(#URL))-1),'.aspx',''),'-',' ') as abc
Which returns below output:
Actual output -
Expected output
i.e i want to eliminate the string after last occurrence of -.
What changes do i have to make to get the expected output..
In all i want a substring after last occurence of / and before last occurence of - as shown above.
Please help and thanks in advance...!
Try this
DECLARE #url VARCHAR (max)='http://v.mercola.com/blogs/public_blog/New-Diet-Pill-Expands-1-000-Times-in-Your-Stomach-24728.aspx'
SELECT Reverse(LEFT(mid, Charindex('/', mid) - 1))
FROM (SELECT Substring(Reverse(#url), Charindex('-', Reverse(#url)) + 1, Len(#url)) AS mid) a
Something like this:
DECLARE #url varchar (max)='http://v.mercola.com/blogs/public_blog/New-Diet-Pill-Expands-1-000-Times-in-Your-Stomach-24728.aspx'
declare #suffix varchar(max)
select #suffix = RIGHT(#URL , CHARINDEX ('/' ,REVERSE(#URL))-1)
select left(#suffix, len(#suffix) - charindex('-', reverse(#suffix)))
Output:
New-Diet-Pill-Expands-1-000-Times-in-Your-Stomach
Another option:
DECLARE #url varchar (max)='http://v.mercola.com/blogs/public_blog/New-Diet-Pill-Expands-1-000-Times-in-Your-Stomach-24728.aspx'
DECLARE #LastSlash int = LEN(#URL) - CHARINDEX('/', REVERSE(#URL)) + 2,
#LastMinus int = LEN(#URL) - CHARINDEX ('-', REVERSE(#URL)) + 1
SELECT SUBSTRING(#URL, #LastSlash, #LastMinus-#LastSlash)

How do I replace a substring of a string before a specific character?

Table Email:
Values:
josh#yahoo.com
carmine32#hotmail.com
zehmaneh#yahoo.com
I want to replace the string before # with test.
Result:
test#yahoo.com
test#hotmail.com
test#yahoo.com
How do I use substringing and replace based on a character in the string?
You don't even need to use substring or replace, you can use this:
SELECT 'test' + RIGHT(email, charindex('#', REVERSE(email)))
FROM YourTable
You can test it out with this:
DECLARE #email nvarchar(50)
SET #email = 'carmine32#hotmail.com'
PRINT 'test' + RIGHT(#email, charindex('#', REVERSE(#email)))
declare #t table(email varchar(30))
insert #t values('josh#yahoo.com'),
('carmine32#hotmail.com'),
('zehmaneh#yahoo.com')
select stuff(email, 1, charindex('#', email), 'Test#')
from #t
Result:
Test#yahoo.com
Test#hotmail.com
Test#yahoo.com
You can use SUBSTRING and CHARINDEX:
UPDATE Email set email =
'test' + SUBSTRING(email, CHARINDEX('#',email), LEN(email))
Fiddle: http://sqlfiddle.com/#!3/0face/6/0
You could
select 'test' + substring(fld, charindex('#', fld), len(fld))

Get everything after and before certain character in SQL Server

I got the following entry in my database:
images/test.jpg
I want to trim the entry so I get: test
So basically, I want everything after / and before .
How can I solve it?
use the following function
left(#test, charindex('/', #test) - 1)
If you want to get this out of your table using SQL, take a look at the following functions that will help you: SUBSTRING and CHARINDEX. You can use those to trim your entries.
A possible query will look like this (where col is the name of the column that contains your image directories:
SELECT SUBSTRING(col, LEN(SUBSTRING(col, 0, LEN(col) - CHARINDEX ('/', col))) + 1,
LEN(col) - LEN(SUBSTRING(col, 0, LEN(col) - CHARINDEX ('/', col))) - LEN(SUBSTRING(
col, CHARINDEX ('.', col), LEN(col))));
Bit of an ugly beast. It also depends on the standard format of 'dir/name.ext'.
Edit:
This one (inspired by praveen) is more generic and deals with extensions of different length:
SELECT SUBSTRING(col, LEN(LEFT(col, CHARINDEX ('/', col))) + 1, LEN(col) - LEN(LEFT(col,
CHARINDEX ('/', col))) - LEN(RIGHT(col, LEN(col) - CHARINDEX ('.', col))) - 1);
Before
SELECT SUBSTRING(ParentBGBU,0,CHARINDEX('/',ParentBGBU,0)) FROM dbo.tblHCMMaster;
After
SELECT SUBSTRING(ParentBGBU,CHARINDEX('-',ParentBGBU)+1,LEN(ParentBGBU)) FROM dbo.tblHCMMaster
----select characters before / including /
select SUBSTRING ('abcde/wxyz',0,CHARINDEX('/','abcde/wxyz')+1)
--select characters after / including /
select SUBSTRING('abcde/wxyz',CHARINDEX('/','abcde/wxyz'),LEN('abcde/wxyz'))
declare #T table
(
Col varchar(20)
)
insert into #T
Select 'images/test1.jpg'
union all
Select 'images/test2.png'
union all
Select 'images/test3.jpg'
union all
Select 'images/test4.jpeg'
union all
Select 'images/test5.jpeg'
Select substring( LEFT(Col,charindex('.',Col)-1),charindex('/',Col)+1,len(LEFT(Col,charindex('.',Col)-1))-1 )
from #T
I have made a method which is much more general :
so :
DECLARE #a NVARCHAR(MAX)='images/test.jpg';
--Touch here
DECLARE #keysValueToSearch NVARCHAR(4000) = '/'
DECLARE #untilThisCharAppears NVARCHAR(4000) = '.'
DECLARE #keysValueToSearchPattern NVARCHAR(4000) = '%' + #keysValueToSearch + '%'
--Nothing to touch here
SELECT SUBSTRING(
#a,
PATINDEX(#keysValueToSearchPattern, #a) + LEN(#keysValueToSearch),
CHARINDEX(
#untilThisCharAppears,
#a,
PATINDEX(#keysValueToSearchPattern, #a) + LEN(#keysValueToSearch)
) -(PATINDEX(#keysValueToSearchPattern, #a) + LEN(#keysValueToSearch))
)
SELECT Substring('ravi1234#gmail.com', 1, ( Charindex('#', 'ravi1234#gmail.com')
- 1 ))
Before,
RIGHT('ravi123#gmail.com', ( Charindex('#', 'ravi123#gmail.com') + 1 ))
After
I just did this in one of my reports and it was very simple.
Try this:
=MID(Fields!.Value,8,4)
Note: This worked for me because the value I was trying to get was a constant not sure it what you are trying to get is a constant as well.
I know this has been a while.. but here is an idea
declare #test varchar(25) = 'images/test.jpg'
select
#test as column_name
, parsename(replace(#test,'/','.'),1) as jpg
,parsename(replace(#test,'/','.'),2) as test
,parsename(replace(#test,'/','.'),3) as images
I found Royi Namir's answer useful but expanded upon it to create it as a function. I renamed the variables to what made sense to me but you can translate them back easily enough, if desired.
Also, the code in Royi's answer already handled the case where the character being searched from does not exist (it starts from the beginning of the string), but I wanted to also handle cases where the character that is being searched to does not exist.
In that case it acts in a similar manner by starting from the searched from character and returning the rest of the characters to the end of the string.
CREATE FUNCTION [dbo].[getValueBetweenTwoStrings](#inputString
NVARCHAR(4000), #stringToSearchFrom NVARCHAR(4000), #stringToSearchTo
NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
BEGIN
DECLARE #retVal NVARCHAR(4000)
DECLARE #stringToSearchFromSearchPattern NVARCHAR(4000) = '%' +
#stringToSearchFrom + '%'
SELECT #retVal = SUBSTRING (
#inputString,
PATINDEX(#stringToSearchFromSearchPattern, #inputString) + LEN(#stringToSearchFrom),
(CASE
CHARINDEX(
#stringToSearchTo,
#inputString,
PATINDEX(#stringToSearchFromSearchPattern, #inputString) + LEN(#stringToSearchFrom))
WHEN
0
THEN
LEN(#inputString) + 1
ELSE
CHARINDEX(
#stringToSearchTo,
#inputString,
PATINDEX(#stringToSearchFromSearchPattern, #inputString) + LEN(#stringToSearchFrom))
END) - (PATINDEX(#stringToSearchFromSearchPattern, #inputString) + LEN(#stringToSearchFrom))
)
RETURN #retVal
END
Usage:
SELECT dbo.getValueBetweenTwoStrings('images/test.jpg','/','.') AS MyResult
I got some invalid length errors. So i made this function, this should not give any length problems. Also when you do not find the searched text it will return a NULL.
CREATE FUNCTION [FN].[SearchTextGetBetweenStartAndStop](#string varchar(max),#SearchStringToStart varchar(max),#SearchStringToStop varchar(max))
RETURNS varchar(max)
BEGIN
SET #string = CASE
WHEN CHARINDEX(#SearchStringToStart,#string) = 0
OR CHARINDEX(#SearchStringToStop,RIGHT(#string,LEN(#string) - CHARINDEX(#SearchStringToStart,#string) + 1 - LEN(#SearchStringToStart))) = 0
THEN NULL
ELSE SUBSTRING(#string
,CHARINDEX(#SearchStringToStart,#string) + LEN(#SearchStringToStart) + 1
,(CHARINDEX(#SearchStringToStop,RIGHT(#string,LEN(#string) - CHARINDEX(#SearchStringToStart,#string) + 1 - LEN(#SearchStringToStart)))-2)
)
END
RETURN #string
END
if Input= pg102a-wlc01s.png.intel.com and Output should be pg102a-wlc01s
we can use below query :
select Substring(pc.name,0,charindex('.',pc.name,0)),pc.name from tbl_name pc
You can try this:
Declare #test varchar(100)='images/test.jpg'
Select REPLACE(RIGHT(#test,charindex('/',reverse(#test))-1),'.jpg','')
Below query gives you data before '-'
Ex- W12345A-4S
SELECT SUBSTRING(Column_Name,0, CHARINDEX('-',Column_Name)) as 'new_name'
from [abc].
Output - W12345A
Inspired by the work of Josien, I wondered about a simplification.
Would this also work? Much shorter:
SELECT SUBSTRING(col, CHARINDEX ('/', col) + 1, CHARINDEX ('.', col) - CHARINDEX ('/', col) - 1);
(I can't test right now because of right issues at my company SQL server, which is a problem in its own right)
Simply Try With LEFT ,RIGHT ,CHARINDEX
select
LEFT((RIGHT(a.name,((CHARINDEX('/', name))+1))),((CHARINDEX('.', (RIGHT(a.name,
((CHARINDEX('/', name))+1)))))-1)) splitstring,
a.name
from
(select 'images/test.jpg' as name)a
declare #searchStart nvarchar(100) = 'search ';
declare #searchEnd nvarchar(100) = ' ';
declare #string nvarchar(4000) = 'This is a string to search (hello) in this text ';
declare #startIndex int = CHARINDEX(#searchStart, #string,0) + LEN(#searchStart);
declare #endIndex int = CHARINDEX(#searchEnd, #string, #startIndex + 1);
declare #length int = #endIndex - #startIndex;
declare #sub nvarchar(4000) = SUBSTRING(#string, #startIndex, #length)
select #startIndex, #endIndex, #length, #sub
This is a little more legible than the one-liners in this answer which specifically answer the question, but not in a generic way that would benefit all readers. This could easily be made into a function as well with a slight modification.
If there are more than one or none occurences of given character use this:
DECLARE #rightidx int = CASE
WHEN 'images/images/test.jpg' IS NULL OR (CHARINDEX('.', 'images/images/test.jpg')) <= 0 THEN LEN('images/images/test.jpg')
ELSE (CHARINDEX('.', REVERSE('images/images/test.jpg')) - 1)
END
SELECT RIGHT('images/images/test.jpg', #rightidx)
This was the approach I took.
CREATE FUNCTION dbo.get_text_before_char(#my_string nvarchar(255),#my_char char(1))
RETURNS nvarchar(255)
AS
BEGIN;
return IIF(#my_string LIKE '%' + #my_char + '%',left (#my_string, IIF(charindex(#my_char, #my_string) - 1<1,1,charindex(#my_char, #my_string) - 1)),'');
END;
CREATE FUNCTION dbo.get_text_after_char(#my_string nvarchar(255),#my_char char(1))
RETURNS nvarchar(255)
AS
BEGIN;
return IIF ( #my_string LIKE '%' + #my_char + '%' ,RIGHT ( #my_string , IIF ( charindex ( #my_char ,reverse(#my_string) )-1 < 1 ,1 ,charindex ( #my_char ,reverse(#my_string) )-1 ) ) , '' )
END;
SELECT
dbo.get_text_before_char('foo-bar','-')
, dbo.get_text_after_char('foo-bar','-')
declare #test varchar(100)='images/test.jpg'
select right(left(#test, charindex('.', #test) - 1),4)