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
Solved

Query Report Builder - don't want negative numbers

Posted on 2011-03-22
7
680 Views
Last Modified: 2012-05-11
Am doing query to create zip code dataset using values in the db. I am coming up with two values that are negative, and I want a list with only valid values. Here's my query:

SELECT
  HAR_CONSUMERS.RES_ZIP
FROM
  HAR_CONSUMERS
WHERE
  HAR_CONSUMERS.RES_ZIP = N'73301'
  OR HAR_CONSUMERS.RES_ZIP >= N'75001'
  AND HAR_CONSUMERS.RES_ZIP <= N'75501'
  OR HAR_CONSUMERS.RES_ZIP >= N'75503'
  AND HAR_CONSUMERS.RES_ZIP <= N'79999'
  OR HAR_CONSUMERS.RES_ZIP >= N'88510'
  AND HAR_CONSUMERS.RES_ZIP <= N'88589'

GROUP BY
  HAR_CONSUMERS.RES_ZIP

ORDER BY
  HAR_CONSUMERS.RES_ZIP

Please help me figure out how to modify the query to NOT get the two negative numbers that are coming up. I assume these values are numbers, but perhaps that's a naive assumption.
Either way, I'd like to strip these two invalid values out.  Thanks in advance!
 How do I delete these 2 values from dataset
0
Comment
Question by:gberkeley
  • 4
  • 3
7 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 35194905
WHat is the data type of RES_ZIP? I am afraid of your AND/OR conditions. Can you try like this?
SELECT DISTINCT HAR_CONSUMERS.RES_ZIP 
    FROM HAR_CONSUMERS 
   WHERE HAR_CONSUMERS.RES_ZIP = 73301 
          OR (HAR_CONSUMERS.RES_ZIP BETWEEN 75001 AND 75501) 
          OR (HAR_CONSUMERS.RES_ZIP BETWEEN 75503 AND 79999) 
          OR (HAR_CONSUMERS.RES_ZIP BETWEEN 88510 AND 88589) 
ORDER BY HAR_CONSUMERS.RES_ZIP

Open in new window

0
 

Author Comment

by:gberkeley
ID: 35195166
Sharath,
I love the elegant efficiency of your syntax!!!  
Alas, the system threw an error, which I suspect gives us another clue.
"Conversion failed when converting the varchar value to data type integer."

It didn't like the 78701-1 values in the dataset (the dash 1 part). Can't tell what happened to the values I wanted to bypass.

Any thoughts?
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35195183
I thought that the data type is INT. You can try this.
SELECT DISTINCT HAR_CONSUMERS.RES_ZIP 
    FROM HAR_CONSUMERS 
   WHERE HAR_CONSUMERS.RES_ZIP = N'73301' 
          OR (HAR_CONSUMERS.RES_ZIP BETWEEN N'75001' AND N'75501') 
          OR (HAR_CONSUMERS.RES_ZIP BETWEEN N'75503' AND N'79999') 
          OR (HAR_CONSUMERS.RES_ZIP BETWEEN N'88510' AND N'88589') 
ORDER BY HAR_CONSUMERS.RES_ZIP

Open in new window

0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:gberkeley
ID: 35195204
Sharath,
SO CLOSE!!! You rock.  Thanks a bunch.
Now we are left with the original problem - and we have the two pesky values to get rid of.
How do I get the dataset of my dreams without these annoying anomalies? These two values have to go
0
 

Author Comment

by:gberkeley
ID: 35195216
One more thought - related thing - would like to have just a list of 5-digit zip codes from which to choose. I want the query to IGNORE all the ones that end in -1 or -2 as well as the ones with the negative prefix and those that are less than 5 digits.  

Any ideas?
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 35195236
check this one.
SELECT DISTINCT HAR_CONSUMERS.RES_ZIP 
    FROM HAR_CONSUMERS 
   WHERE (HAR_CONSUMERS.RES_ZIP = N'73301' 
           OR (HAR_CONSUMERS.RES_ZIP BETWEEN N'75001' AND N'75501') 
           OR (HAR_CONSUMERS.RES_ZIP BETWEEN N'75503' AND N'79999') 
           OR (HAR_CONSUMERS.RES_ZIP BETWEEN N'88510' AND N'88589')) 
         AND HAR_CONSUMERS.RES_ZIP NOT LIKE '-%' 
         AND LEN(HAR_CONSUMERS.RES_ZIP) >= 5 
ORDER BY HAR_CONSUMERS.RES_ZIP

Open in new window

0
 

Author Closing Comment

by:gberkeley
ID: 35196544
Sharth, thank you SO much!!!! Well done!!!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Written by Valentino Vranken. A while ago I wrote an article called Chart Optimization Tips (http://www.experts-exchange.com/articles/Microsoft/Development/MS-SQL-Server/MS-SQL_Reporting/Chart-Optimization-Tips.html).  This article explained how …
Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 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