Solved

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

Posted on 2014-10-27
5
106 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 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
replicated - directional or bidirectional? 3 29
Help Required 3 95
Inserting oldest record into new table. 5 23
SQL Query Help Top 1 and Distinct? 6 22
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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

776 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