Solved

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

Posted on 2014-10-27
5
109 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: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 65

Expert Comment

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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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