?
Solved

Query Report Builder - don't want negative numbers

Posted on 2011-03-22
7
Medium Priority
?
692 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 41

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 41

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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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 41

Accepted Solution

by:
Sharath earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction As you'll probably know, a data region in a SQL Server Reporting Services report can be linked to only one dataset.  This makes it troublesome when you need to display data from more than one dataset in the same data region.  SQL Serve…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 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