Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select left of character

Author  Topic 

masterdineen
Aged Yak Warrior

550 Posts

Posted - 2012-11-22 : 06:53:48
Hello there

i am trying to work out how i can select left of a character.

eg

emailaddress@hotmail.com;emailaddress2@hotmail.com

i want to select the address to the left of ';'

stepson
Aged Yak Warrior

545 Posts

Posted - 2012-11-22 : 07:03:22

declare @str as varchar(500)
set @str ='emailaddress@hotmail.com;emailaddress2@hotmail.com'
select charindex(';',@str,1), substring(@str, 1,charindex(';',@str,1)-1), left(@str,charindex(';',@str,1)-1)

Ce-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mut
sabinWeb
Go to Top of Page

masterdineen
Aged Yak Warrior

550 Posts

Posted - 2012-11-22 : 09:35:14
silly question, but what about everything to the right of ';'
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-22 : 10:36:01
One of these two; I usually prefer the first one:
DECLARE @str AS VARCHAR(500)
SET @str = 'emailaddress@hotmail.com;emailaddress2@hotmail.com'
SELECT STUFF(@str, 1, CHARINDEX(';', @str, 1), ''),
RIGHT(@str, LEN(@str) -CHARINDEX(';', @str, 1))
Go to Top of Page

masterdineen
Aged Yak Warrior

550 Posts

Posted - 2012-11-22 : 10:55:16
thank you very much
Go to Top of Page
   

- Advertisement -