Solved

Query Report Builder - don't want negative numbers

Posted on 2011-03-22
7
678 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

How to use Variables  and Custom code in SSRS report and Assembly reference to use compile shared code in SSRS. Its big question for all who are working with SSRS. It is easy to create assembly and refer in SSRS report, still there are some steps…
Hi All, I am here to write a simple article to move SSRS (SQL Server Reporting Services) reports from one server to another. When I have faced the same issue to move reports those were developed by developer on development server and now need to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

14 Experts available now in Live!

Get 1:1 Help Now