Solved

Query Report Builder - don't want negative numbers

Posted on 2011-03-22
7
683 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
[X]
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
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

In this short article I will be talking about two functions in the SQL Server Reporting Services (SSRS) function stack.  Those functions are IIF() and Switch().  And I'll be showing you how easy it is to add an Else part to the Switch function. T…
Introduction In the following article I’ll be discussing and demonstrating several different ways of how images can be put on a report. I’m using SQL Server Reporting Services 2008 R2 CTP, more precisely version 10.50.1352.12, but the methods ex…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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