Solved

Query Report Builder - don't want negative numbers

Posted on 2011-03-22
7
673 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:gberkeley
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sharth, thank you SO much!!!! Well done!!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
It is helpful to note: This is a cosmetic update and is not required, but should help your reports look better for your boss.  This issue has manifested itself in SSRS version 3.0 is where I have seen this behavior in.  And this behavior is only see…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

16 Experts available now in Live!

Get 1:1 Help Now