473,470 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SELECT CASE WHEN IN() statement

92 New Member
Hi,

I have a query that is sorting people into their respective areas of the country and need to assign each postcode the correct label (Eg: 'North', 'Wales', etc.). I am trying to do this in a CASE statement at the moment but it does not like the IN() part, returning all the values with the 'N/A' label. Everything parses fine and the query runs, it just doesn't assign the correct labels.

Expand|Select|Wrap|Line Numbers
  1. SELECT    PH.SERIALNUMBER,
  2.     PH.PLEDGEID,
  3.     PH.INSTALMENTVALUE AS AVERAGE,
  4.  
  5.     CASE WHEN C.POSTCODE IN ('KW%','HS%','IV%','PH%','AB%','DD%','PA%','KA%','FK%','KY%','EH%','TD%','DG%','ML%','G%','ZE%') THEN 'SCOTLAND'
  6.     WHEN C.POSTCODE IN ('NE%','CA%','DH%','SR%','TS%','DL%','LA%','IM%','FY%','PR%','BB%','BD%','HG%','YO%','LS%','HX%','OL%','BL%','WN%','L%','CH%','CW%','WA%','M%','SK%','S%','HD%','WF%','DN%','HU%') THEN 'NORTH'
  7.     WHEN C.POSTCODE IN ('BT%') THEN 'NORTHERN IRELAND'
  8.     WHEN C.POSTCODE IN ('LL%','SY%','LD%','SA%','NP%','CF%') THEN 'WALES'
  9.     WHEN C.POSTCODE IN ('TF%','ST%','DE%','NG%','PE%','LE%','WS%','WV%','DY%','HR%','WR%','B%','CV%','NN%') THEN 'MIDLANDS'
  10.     WHEN C.POSTCODE IN ('TR%','PL%','TQ%','EX%','TA%','DT%','BA%','BS%','GL%','OX%','SN%','SP%','BH%') THEN 'SOUTH WEST'
  11.     WHEN C.POSTCODE IN ('NR%','IP%','CB%','CO%','SG%','MK%','LU%','HP%','AL%','WD%','EN%','CM%','RG%','SL%','GU%','SO%','PO%','BN%','RH%','TN%','ME%','CT%','SS%','GY%','JE%') THEN 'SOUTH EAST'
  12.     WHEN C.POSTCODE IN ('HA%','UB%','TW%','KT%','NW%','W%','SW%','SM%','CR%','N%','WC%','EC%','SE%','BR%','DA%','E%','IG%','RM%') THEN 'LONDON'
  13.     ELSE 'N/A'
  14.     END AS REGION,
  15.  
  16.     CASE WHEN PH.SERIALNUMBER IN (SELECT DISTINCT(BD1.SERIALNUMBER) FROM BATCHDETAIL BD1 WHERE ((BD1.DESTINATIONCODE) IN('sp%','ch%','sum%','may%','AUT%') AND ((BD1.DATEOFPAYMENT) <= GETDATE() AND (BD1.DATEOFPAYMENT) >= (dateadd(year,-1,getdate()))))) THEN 1 ELSE 0 END AS APPEALS,
  17.  
  18.     C.EMAILADDRESS AS EMAILS,
  19.     C.DATEOFBIRTH AS DOBS
  20.  
  21.     FROM (PLEDGEHEADER PH INNER JOIN BATCHDETAIL BD ON PH.PLEDGEID = BD.PLEDGEID) INNER JOIN CONTACT C ON PH.SERIALNUMBER = C.SERIALNUMBER
  22.     WHERE (((PH.PAYMENTFREQUENCY)='monthly') AND ((PH.PLEDGESTATUS)='active') AND ((BD.DESTINATIONCODE) IN('_ptr','_sto','pe%','tc01','pcy08','mjp','wap','ps059','scp','ps%','sp%','ch%','sum%','may%','AUT%')))
When I change the WHEN to
Expand|Select|Wrap|Line Numbers
  1. WHEN C.POSTCODE LIKE 'HA%' OR C.POSTCODE LIKE 'UB%' OR C.POSTCODE LIKE 'TW%' etc
the labels are properly assigned but the whole statement is significantly longer and, not going into the details of why, doesn't fit into the SQL editior in the report manager that is used here.

It is a ridiculous limit on the queries that can be made, but it is the way it is, and this one would fit if the IN() statement worked.

Any help is much appreciated,

NDayave
Mar 29 '10 #1
4 25202
jvskarthick
13 New Member
IN Statement "Determines whether a specified value matches any value in a sub query or a list". We can't use the IN with ('KW%'). If you are using % then you need to use LIKE operator only.
If you want to use the IN then give the full value inside the IN (‘Full Name stored in Table’), otherwise in your table you can add the column Region in existing table “CONTACT C” and bring the values directly from it.

Thanks,
JK
Mar 29 '10 #2
NDayave
92 New Member
Cheers for that, made it "LEFT(POSTCODE,2) IN ('KW', 'W1', 'W2', etc)" which also clears up some ambiguity in the selection process Eg: 'L%' and 'LA%'

Thanks again,

NDayave
Mar 30 '10 #3
Rikkip
2 New Member
You could use Substr(POSTCODE,1,4) IN ('W1', 'W2', etc)

This would give the first 4 characters of the postcode and be more accurate than 'W1%', 'W2%',
Oct 27 '10 #4
ck9663
2,878 Recognized Expert Specialist
If that's the case, put those postcode in a table and use EXISTS to make it faster...

Happy Coding!!!

~~ CK
Oct 27 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: gilgantic | last post by:
HELP!!! I am trying to fine tune or rewrite my SELECT statement which has a combination of SUM and CASE statements. The values are accurate, but the query is slow. BUSINESS RULE =============...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
2
by: Ini | last post by:
Hi, I have a recordset containing names of photographs and descriptions of these photo's. For exempla: Photo: car01.jpg Description: Red Corvette
4
by: deko | last post by:
When I loop through this function, it works fine until it hits End Function - then it jumps to End Select. Very strange... This behavior occurs when Case = 255. Any ideas why this is happening? ...
10
by: MLH | last post by:
Suppose the following... Dim A as Date A=#7/24/2005# I wish to compare value of A against 2 other values: 1) 8/1/2005 2) 9/1/2005 Which is better and why... First:
8
by: | last post by:
Hello, This is gonna sound real daft, but how do I test a Select Case statement for variants of a theme? Here's a snippet of my code... Select Case sUsr Case "Guest", "TsInternetUser",...
2
by: scole954387 | last post by:
Hi, I have a problem. I have written a SQL statement that has a nested select case statement on the 'where' clause to condition the results. ...
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
3
by: fanoftvb | last post by:
Hi, i have problem with my select case statement. When i click on the button, nothing happened. Can someone help me see where i go wrong, thanks a lot. :) Sub GridView1_RowCommand(ByVal sender...
1
by: EORTIZ | last post by:
I would like to know the way that I can write a CASE function in a select statement that uses another variable that is created in the same SELECT statement using another CASE function. This is...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.