Solved

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

Posted on 2014-10-27
5
103 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:chrismichalczuk
5 Comments
 
LVL 65

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 500 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:chrismichalczuk
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 65

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now