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)
 Query leftmost characters of email field

Author  Topic 

fralo
Posting Yak Master

161 Posts

Posted - 2012-01-25 : 17:52:03
Any other day I would know how to do this.

I'm trying to figure out how to get all characters left of the '@' in an email field.

select email from table

Thanks for your help.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-01-25 : 18:36:29
select left('name@domain.com',charindex('@','name@domain.com')-1)

select left(email,charindex('@',email)-1) from table


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2012-01-25 : 18:42:51
or

select substring('name@domain.com',1, charindex('@','name@domain.com')-1)

If you don't have the passion to help people, you have no passion
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-26 : 09:12:10
i hope email values are consistent with @ included in them, if not you need a case check to see if its present or not otherwise substring will break.

select substring(email,1,case when charindex('@',email)>0 then charindex('@',email) else len(email)-1 end)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2012-01-26 : 10:05:12
That worked. Thanks so much for your help.

I only had to add NULLIF.

SELECT left(a.email,nullif(charindex('@',email)-1,-1))
Go to Top of Page
   

- Advertisement -