473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Using LIKE in a CASE statement

I have a unique situation. My data looks something like:

Test 1
Test 2
Test 3
Test 2
Test 1
Test 300
Test 200
Test 1
Test 300
Test 200

I want to display all of the above like:

Test 1
Test 2
Test 3
Test 2
Metals
Test 1
Test 1

Notice that I have everything displayed except for 'Test 200' and
'Test 300'. This have been replaced with the word 'Metals' and I'm
only displaying it one time, no matter how often Test 200 shows up, or
Test 300.

My code looks like the following. It works good, except if I have to
add 'Test 400' etc...I would have to hard code them and it will build
up real quick. I looked at GROUPs to see if that would help, but I
don't think it would because it has to be displayed with a different
name such as 'Metals' for all the tests...Test 100, 200, etc... Is
there a better way without having to add each number in it? I think
the best way is to use a LIKE statment where the WHEN is being used
but I keep getting errors if I use the LIKE sytnax where the WHEN is
being used.

SELECT sample
FROM mysamples
WHERE (sample <'TEST 200') AND (sample <'TEST 300')

UNION ALL

SELECT DISTINCT
SAMPLE = CASE Sample WHEN 'test 200' THEN 'Metals' WHEN 'Test 300'
THEN 'Metals'
END
FROM MYSAMPLES
WHERE (sample = 'TEST 200') OR
(sample = 'TEST 300')

Any help is appreciated...

Feb 3 '07 #1
4 9792
(al**************@gmail.com) writes:
Notice that I have everything displayed except for 'Test 200' and
'Test 300'. This have been replaced with the word 'Metals' and I'm
only displaying it one time, no matter how often Test 200 shows up, or
Test 300.

My code looks like the following. It works good, except if I have to
add 'Test 400' etc...I would have to hard code them and it will build
up real quick. I looked at GROUPs to see if that would help, but I
don't think it would because it has to be displayed with a different
name such as 'Metals' for all the tests...Test 100, 200, etc... Is
there a better way without having to add each number in it? I think
the best way is to use a LIKE statment where the WHEN is being used
but I keep getting errors if I use the LIKE sytnax where the WHEN is
being used.
Without knowing the exact rules, it's difficult to give a whole-
covernig answer. But the condition

sample LIKE 'TEST [0-9][0-9]{0-9]'

could be used to handle all with Test plus a three-digit number.

SELECT sample
FROM mysamples
WHERE sample NOT LIKE 'TEST [0-9][0-9]{0-9]'
UNION ALL
SELECT DISTINCT 'Metals'
FROM MYSAMPLES
WHERE sammple LIKE LIKE 'TEST [0-9][0-9]{0-9]'

But I would suspect that your real business problem have a different
solution.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 3 '07 #2
>I have a unique situation. <<

Probably not!! Trust me!
>My data looks something like: <<
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.

Feb 3 '07 #3
On Feb 3, 2:19 pm, "--CELKO--" <jcelko...@earthlink.netwrote:
I have a unique situation. <<

Probably not!! Trust me!
My data looks something like: <<

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.
Thanks Erland, your solution did it. I don't understand how the
second part of the syntax: SELECT DISTINCT 'Metals'
FROM MYSAMPLES

shows up as 'METALS'. I thought i would have had to select an AS
somewhere...nonetheless it worked!!

Feb 4 '07 #4
(al**************@gmail.com) writes:
Thanks Erland, your solution did it. I don't understand how the
second part of the syntax: SELECT DISTINCT 'Metals'
FROM MYSAMPLES

shows up as 'METALS'. I thought i would have had to select an AS
somewhere...nonetheless it worked!!
Not sure what you mean. If it says METALS in all uppercase in the output,
something very strange is going on.

If you mean the column name, in a SELECT UNION statement the column
names are derived from the first SELECT, so there is no need to
provide column names for the subsequent SELECT:s.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the...
7
by: Alex Vorobiev | last post by:
hi there, i am using sql server 7. below is the stored procedure that is giving me grief. its purpose it two-fold, depending on how it is called: either to return a pageset (based on page...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: Bob | last post by:
Currently I am using this statement to translate 3 fields in my db thru Visual Basic. I import the data from one table to another then call the IFF statements and the NewDate to translate the...
5
by: Colleyville Alan | last post by:
I have built a SQL statement that is trying to loop through the fields of a table that was built from a spreadsheet and hence is "short and fat". So rather than hard-coding, I have a loop from...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
12
by: rAinDeEr | last post by:
Hi, I have a table with 2 columns ** CREATE TABLE test (emp_num DECIMAL(7) NOT NULL,emp_name CHAR(10) NOT NULL) and i have inserted a number of records. ** Now, I want to insert a new...
1
by: Thiero | last post by:
Hi I posted s thread but did have any reply, I am a new programmer and really wants someone to help me on how to use TreeMap for this code cos I want to it to be able to handle the options from 6...
1
by: tomlebold | last post by:
Having problems displaying query results from combo boxes on a sub form, which is on the same form that is used to select criteria. This has always worked form me when displaying query results on...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.