Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

what's wrong with this ? - sum(if contains "@", [Email]) then 1 else 0 end

Posted on 2014-10-27
5
Medium Priority
?
122 Views
Last Modified: 2015-01-07
I need to do a calculated field that basically tells me how many emails exist in a field called email.
I have 36k records and want to do a quick split between how many have emails and how many don't

I've used this but it fails

sum(if contains "@", [Email]) then 1 else 0 end
0
Comment
Question by:Chris Michalczuk
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40406752
Copy-paste the below code into your SSMS, execute it to verify it does what you need, and then modify it to fit your needs.  Uses LIKE and underscores to validate email addresses.
IF OBJECT_ID('tempdb..#email') IS NOT NULL
   DROP TABLE #email
GO

CREATE TABLE #email (addr nvarchar(50)) 

INSERT INTO #email (addr) 
VALUES 
   ('a@b.c'), 
   ('goo@foo.boo'), 
   ('@dabba.doo'), 
   ('yada@yada'),
   ('jim@jimbo.biz'),
   ('not an email address') 

-- Return the whole set
SELECT * FROM #email

-- Underscore operator - Means any single character, so @dabba.doo and yada@yada are not returned. 
-- Correctly formatted email addresses:
SELECT * 
FROM #email
WHERE addr LIKE '_%@_%._%'

Open in new window

0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 1500 total points
ID: 40406756
Try

sum(iif(EmailAddress like '%@%', 1,0))
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40406764
select case when [email] like '%@%' then 1 else 0 end from mytable

Open in new window


or

select count(email) from mytable where email like '%@%'

Open in new window

0
 

Author Comment

by:Chris Michalczuk
ID: 40407792
my app (Tableau) returns this
expect closing parenthesis or comma while parsing arguement list for iif

for
sum(iif(EmailAddress like '%@%', 1,0))
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40535734
Was this a Tableau question?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question