I have the data in column like this 12 address, i want to eliminate numeric with address how can i do to achieve the result Address. Here can be any other string also not only address.
Query:
SELECT Address FROM TABLE
Thanks
In SQL Server:
SELECT
COALESCE(LTRIM(SUBSTRING(Address, FirstPos, 2147483647)), '') AS Address
FROM (
SELECT
Address,
NULLIF(PATINDEX('%[^0-9]%', Address), 0) AS FirstPos
FROM atable
) s
SELECT SUBSTRING(address, CHARINDEX(' ',address)+1,field_len)
FROM table
Explained
Syntax: SUBSTRING(fieldname, start_position, end_position)
In your case
SUBSTRING(
address -- your field name
, CHARINDEX(' ',address)+1 -- find the space char
, field_len -- get all the rest of the string
)
SELECT RIGHT(Address,LEN(Address) - CHARINDEX (' ', Address) + 1) FROM tbl
Something like this should work.
Related
I try to get first name, last name, provider from an email address "ben.ghol#gmail.com" but it doesn't work.
Table emailtable.
My code here:
select
SUBSTR(email,1, INSTR(email,'.')-1) as firstname,
SUBSTR(email,INSTR(email,'.')+1, INSTR(email,'#')-1) as lastname,
SUBSTR(email,INSTR(email,'#')+1,INSTR(email,'.')-1) as "provider"
from emailtable;
The third argument for SUBSTR is the length of the substring (and not the position of the end character).
You can use a nested sub-query to find the position of the separators and then use those values in SUBSTR in the outer query:
SELECT SUBSTR(email, 1, name_separator - 1) AS first_name,
SUBSTR(email, name_separator + 1, at_separator - name_separator - 1)
AS last_name,
SUBSTR(email, at_separator + 1, domain_separator - at_separator - 1)
AS domain
FROM (
SELECT email,
INSTR(email, '.') AS name_separator,
INSTR(email, '#') AS at_separator,
INSTR(email, '.', INSTR(email, '#')) AS domain_separator
FROM emailtable
)
Which, for the sample data:
CREATE TABLE emailtable ( email ) AS
SELECT 'ben.ghol#gmail.com' FROM DUAL;
Outputs:
FIRST_NAME
LAST_NAME
DOMAIN
ben
ghol
gmail
db<>fiddle here
You can use regexp_substr():
select regexp_substr(email, '^[^.]+') as firstname,
regexp_substr(email, '[^.#]+', 1, 2) as lastname,
regexp_+substr(email, '[^#]+$') as provider
from (select 'ben.ghol#gmail.com' as email from dual) x;
Here is a db<>fiddle.
SUBSTR takes 3 parameters: input string, start position and length of substring.
You appear to be using end position as the 3rd parameter.
You can get the length to use as the 3rd parameter by doing (end position - start position + 1)
I have a column email with multiple delimiters like space ,/ , .
email
/john#thundergroup.com.mi/chris#cup.com.ey
r.Info#bc.com / rudi.an#yy.com
Dal#pema.com/Al#ama.com
/randi#mv.com
zul#sd.com/sat#sd.com/ faze#sd.com
My query:
select email,
CASE WHEN CHARINDEX(' ', email) > 0 THEN SUBSTRING(email, 0, CHARINDEX(' ', email)) ELSE
email END as Emailnew
FROM table
my output:
/john#thundergroup.com.mi/chris#cup.com.ey
r.Info#bc.com
Dal#pema.com/Al#ama.com
/randi#mv.com
zul#sd.com/sat#sd.com/ faze#sd.com
Please suggest changes so that in a single query I'm able to extract email
To get the first email always, you can try this below logic-
DEMO HERE
SELECT
CASE
WHEN CHARINDEX('/',email,2) = 0 THEN REPLACE(email,'/','')
ELSE REPLACE(SUBSTRING(email,0,CHARINDEX('/',email,2)),'/','')
END
FROM your_table
Output will be-
john#thundergroup.com.mi
r.Info#bc.com
Dal#pema.com
randi#mv.com
zul#sd.com
On modern SQL Servers try something like:
-- Setup...
create table dbo.Foo (
FooID int not null identity primary key,
Email nvarchar(100)
);
insert dbo.Foo (Email) values
('/john#thundergroup.com.mi/chris#cup.com.ey'),
('r.Info#bc.com / rudi.an#yy.com'),
('Dal#pema.com/Al#ama.com'),
('/randi#mv.com'),
('zul#sd.com/sat#sd.com/ faze#sd.com');
go
-- Demo...
select FooID, [Email]=value
from dbo.Foo
outer apply (
select top 1 value
from string_split(translate(Email, ' /', ';;'), ';')
where nullif(value, '') is not null
) Splitzville;
Which yields:
FooID Email
1 john#thundergroup.com.mi
2 r.Info#bc.com
3 Dal#pema.com
4 randi#mv.com
5 zul#sd.com
Requirements:
SQL Server 2016 and later for string_split().
SQL Server 2017 and later for translate().
If you want the first email only, use patindex():
select email,
left(email, patindex('%[^a-zA-Z0-9#.]%', email + ' ') - 1) as Emailnew
from table;
The pattern (a-zA-Z0-9#.) are valid email characters. You may have additional ones that you care about.
Unfortunately, I notice that some of your lists start with delimiter characters. In my opinion, the above works correctly by returning an empty value. That said, your desired results are to get the second value in that case.
So, you have to start the search at the first valid email character:
select t.email,
left(v.email1, patindex('%[^-_a-zA-Z0-9#.]%', v.email1 + ' ') - 1) as Emailnew
from t cross apply
(values (stuff(t.email, 1, patindex('%[-_a-zA-Z0-9#.]%', t.email) - 1, ''))) v(email1);
Here is a db<>fiddle.
I'm having a table Employee, in that some values are started with ", ". So, I need to remove the comma and white-space at the beginning of the name at the time of SELECT query using LTRIM() - SQL-Server.
My Table : Employee
CREATE TABLE Employee
(
PersonID int,
ContactName varchar(255),
Address varchar(255),
City varchar(255)
);
INSERT INTO Employee(PersonID, ContactName, Address, City)
VALUES ('1001',', B. Bala','21, Car Street','Bangalore');
SELECT PersonID, ContactName, Address, City FROM Employee
Here the ContactName Column has a value ", B. Bala". I need to remove the comma and white-space at the beginning of the name.
Alas, SQL Server does not support the ANSI standard functionality of specifying the characters for LTRIM().
In this case, you can use:
(case when ContactName like ', %' then stuff(ContactName, 1, 2, '')
else ContactName
end)
You could potentially use PATINDEX() in order to get this done.
DECLARE #Text VARCHAR(50) = ', Well Crap';
SELECT STUFF(#Text, 1, PATINDEX('%[A-z]%', #Text) - 1, '');
This would output Well Crap. PATINDEX() will find first letter in your word and cut everything before it.
It works fine even if there's no leading rubbish:
DECLARE #Text VARCHAR(50) = 'Mister Roboto';
SELECT STUFF(#Text, 1, PATINDEX('%[A-z]%', #Text) - 1, '');
This outputs Mister Roboto
If there are no valid characters, let's say ContactName is , 9132124, :::, this would output NULL, if you'd like to get blank result, you can use COALESCE():
DECLARE #Text VARCHAR(50) = ', 9132124, :::';
SELECT COALESCE(STUFF(#Text, 1, PATINDEX('%[A-z]%', #Text) - 1, ''), '');
This will output an empty string.
You could also use REPLACE.....
eg.
REPLACE( ' ,Your String with space comma', ' ,', '')
UPDATE dbo.Employee
SET
dbo.Employee.ContactName = replace(LEFT(ContactName, 2),', ','')
+ SUBSTRING (ContactName, 3, len(contactname))
where LEFT(ContactName, 2)=', '
This will only update where first two character contains ', '
If I have a column in which strings vary in length but they ALL have a slash \ within,
how can I SELECT to have one column display everything BEFORE the \ and another column displaying everything AFTER the \?
name column1 column2
DB5697\DEV DB5697 DEV
I have seen CHARINDEX and REVERSE on MSDN but haven't been able to put together a soltuion.
How can I best split a varchar/string column value into 2 columns in a result set in TSQL ?
what about using PARSENAME function in a tricky way?
USE tempdb;
GO
CREATE TABLE #names
(
id int NOT NULL PRIMARY KEY CLUSTERED
, name varchar(30) NOT NULL
);
GO
INSERT INTO #names (id, name)
VALUES
(1, 'DB5697\DEV'),
(2, 'DB5800\STG'),
(3, 'DB5900\PRD');
GO
SELECT
name
, PARSENAME(REPLACE(name, '\', '.'), 2) AS [Server]
, PARSENAME(REPLACE(name, '\', '.'), 1) AS [Instance]
FROM
#names;
GO
DROP TABLE #names;
GO
The PARSENAME function accepts 2 parameters and gets the name part of a fully qualified name. The second parameter is the part name enumerator.
Value 2 is for SCHEMA and 1 is for OBJECT.
So, with the REPLACE function the "\" char is replaced by "." in order to have a SCHEMA.OBJECT format of your SERVERNAME\INSTANCE values. Then, PARSENAME behave like having a simple object name in the string.
How about the following (SQL Fiddle):
SELECT m.name,
LEFT(m.name, CHARINDEX('\', m.name) - 1) AS column1,
RIGHT(m.name, LEN(m.name) - CHARINDEX('\', m.name)) AS column2
FROM MyTable m
How to handle strings with no \ in them (SQL Fiddle):
SELECT m.name,
CASE WHEN CHARINDEX('\', m.name) = 0 THEN ''
ELSE LEFT(m.name, CHARINDEX('\', m.name) - 1) END AS column1,
CASE WHEN CHARINDEX('\', m.name) = 0 THEN ''
ELSE RIGHT(m.name, LEN(m.name) - CHARINDEX('\', m.name)) END AS column2
FROM MyTable m;
You can use CHARINDEX to check for the character position of the splitter ('/') and use SUBSTRING to split the string.
However care has to be taken to ensure you handle records without splitters else you would invoke an error.
Also in the case where splitter is unavailable, decision has to be made as to which column the data should be mapped to. Here I am mapping data to FirstName and assigning NULL to LastName
DECLARE #TableBuyer TABLE (ID INT, FullName VARCHAR(100))
INSERT INTO #TableBuyer
SELECT '1','Bryan/Greenberg' UNION ALL
SELECT '2','Channing/Tatum' UNION ALL
SELECT '3','Paul/William' UNION ALL
SELECT '4','EricBana' UNION ALL
SELECT '5','James/Lafferty' UNION ALL
SELECT '6','Wentworth/Miller'
SELECT
CASE
WHEN CHARINDEX('/', FullName) > 0 THEN SUBSTRING(FullName, 1, CHARINDEX('/', FullName) - 1)
ELSE FullName
END AS FirstName
,
CASE
WHEN CHARINDEX('/', FullName) > 0 THEN SUBSTRING(FullName, CHARINDEX('/', FullName) + 1, LEN(FullName))
ELSE NULL
END AS LastName
FROM #TableBuyer;
DECLARE #TableBuyer TABLE (ID INT, FullName VARCHAR(100))
INSERT INTO #TableBuyer
SELECT '1','Bryan/Greenberg' UNION ALL
SELECT '2','Channing/Tatum' UNION ALL
SELECT '3','Paul/William' UNION ALL
SELECT '4','EricBana' UNION ALL
SELECT '5','James/Lafferty' UNION ALL
SELECT '6','Wentworth/Miller'
select left(FullName, len(FullName)-CHARINDEX('/', REVERSE(FullName))) as firstname,
substring(FullName, len(FullName)-CHARINDEX('/', REVERSE(FullName))+ 2, len(FullName)) as lastname
from #TableBuyer
OR
select left(FullName, len(FullName)-CHARINDEX('/', REVERSE(FullName))) as firstname,
RIGHT(FullName, len(FullName)-CHARINDEX('/', FullName)) as lastname
from #TableBuyer
There is no "simple" method. Something like this should work:
select left(col, charindex('\', col) - 1) as column1,
right(col, charindex('\', reverse(col)) - 1) as column2
You might need to double up on the backslash ('\\') to get it to work properly.
i have column in a table (column name is “from”), looks like this
blabla#hotmail.com
frank ocean real frankocean#mail.com
ari#gold.com
frits west f.west#mail.com
I want to select the email addresses only, how do i do this? with a substring?
I can find a domain, but i want to have the complete mail addresses, like this:
blabla#hotmail.com
frankocean#mail.com
ari#gold.com
f.west#mail.com
thanks!
Reverse the string and look for a space before the address: try this
CREATE TABLE #Addresses (EmailAddress VARCHAR(100))
INSERT INTO #Addresses (EmailAddress)
SELECT 'blabla#hotmail.com'
UNION
SELECT 'frank ocean real frankocean#mail.com'
UNION
SELECT 'ari#gold.com'
UNION
SELECT 'frits west f.west#mail.com'
SELECT LTRIM(RTRIM(RIGHT(EmailAddress, CHARINDEX(' ', REVERSE(' ' + EmailAddress),CHARINDEX('#', REVERSE(' '+emailAddress)))))) FROM #Addresses
EDIT: if you have any strings that contain the name after the address, you can use the following to strip out the address:
CREATE TABLE #Addresses (EmailAddress VARCHAR(100))
INSERT INTO #Addresses (EmailAddress)
SELECT 'blabla#hotmail.com'
UNION
SELECT 'frank ocean real frankocean#mail.com'
UNION
SELECT 'ari#gold.com'
UNION
SELECT 'frits west f.west#mail.com'
UNION
SELECT 'me#me.com my name'
SELECT LTRIM(RTRIM(LEFT(RIGHT(EmailAddress, CHARINDEX(' ', REVERSE(' ' + EmailAddress),CHARINDEX('#', REVERSE(' '+emailAddress)))), CHARINDEX(' ', EmailAddress + ' '))) FROM #Addresses
DROP TABLE #Addresses
EDIT 2: forgot to add trimming functions
EDIT 3: final code using the OP's column name (table name not posted):
SELECT LTRIM(RTRIM(LEFT(RIGHT([From], CHARINDEX(' ', REVERSE(' ' + [From]),CHARINDEX('#', REVERSE(' '+[From])))), CHARINDEX(' ', [From]+ ' '))) FROM -- whatever your table is named
If the output is always in the format that you gave, then hopefully this will work:
SELECT RIGHT([From], CHARINDEX(' ', REVERSE(' ' + [From])) - 1) AS [Result]
FROM YourTable
This will only work if there is a space before the input that you want (the actual email address). I use this for a similar purpose in for some legacy dodgy customer data.
This is for SQL Server, I don't know if it will work for any other RDBMS.