473,400 Members | 2,163 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,400 software developers and data experts.

Using Case within Select

Hello,
I'm working on a DB on which I can run only SELECT commands, so I'd
like to modify this UPDATE command, which makes use of CASE:

update Table
set field2 = case
when (SUBSTR(field,1,2) in ('01','02','05')) then 1
when (SUBSTR(field,1,2) in ('15','16')) then 2
when (SUBSTR(field,1,2) in ('18','19','22','36') or SUBSTR(field,1,3)
in ('174','175','177','244','245','297','323','334',' 335')) then 3
else 0
end;

I've modified it into this:
select *,
case
when (SUBSTR(field,1,2) in ('01','02','05')) then 1
when (SUBSTR(field,1,2) in ('15','16')) then 2
when (SUBSTR(field,1,2) in ('18','19','22','36') or SUBSTR(field,1,3)
in ('174','175','177','244','245','297','323','334',' 335')) then 3
else 0
end
as field2
from Table

But it doesn't work properly, because it always returns 0.
So I thought to change it into this:
select *,
case SUBSTR(field,1,2)
when '01' then '1'
when '02' then '1'
when '05' then '1'
when '15' then '2'
when '16' then '2'
when '18' then '3'
when '19' then '3'
when '22' then '3'
when '36' then '3'
else 0
end
as field2
from Table

But, in this way, I only check 2 chars values, even if I need to check
3 chars values too...
How can I do? Have you got any idea?

Thank you very much in advance
Cheers
Carmelo
Jun 27 '08 #1
2 5993
Please do not multi-post, replied in .programming, copy/paste here:

There is no SUBSTR function in SQL Server, the correct function is
SUBSTRING. The CASE expression is correct and should return the values if
your data has the expected strings.

Try this for a test:

CREATE TABLE Foo (
keycol INT PRIMARY KEY,
datacol VARCHAR(10));

INSERT INTO Foo VALUES(1, '0123456789');
INSERT INTO Foo VALUES(2, '1523456789');
INSERT INTO Foo VALUES(3, '2234567890');
INSERT INTO Foo VALUES(4, '2454567890');
INSERT INTO Foo VALUES(5, '9994567890');

SELECT keycol,
CASE
WHEN SUBSTRING(datacol, 1, 2) IN ('01', '02', '05') THEN 1
WHEN SUBSTRING(datacol, 1, 2) IN ('15', '16') THEN 2
WHEN SUBSTRING(datacol, 1, 2) IN ('18', '19', '22', '36')
OR SUBSTRING(datacol, 1, 3) IN ('174', '175', '177', '244',
'245', '297', '323', '334', '335')
THEN 3
ELSE 0
END AS col2
FROM Foo;

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #2
On 7 Giu, 15:37, "Plamen Ratchev" <Pla...@SQLStudio.comwrote:
Please do not multi-post, replied in .programming, copy/paste here:

There is no SUBSTR function in SQL Server, the correct function is
SUBSTRING. The CASE expression is correct and should return the values if
your data has the expected strings.

Try this for a test:

CREATE TABLE Foo (
keycol INT PRIMARY KEY,
datacol VARCHAR(10));

INSERT INTO Foo VALUES(1, '0123456789');
INSERT INTO Foo VALUES(2, '1523456789');
INSERT INTO Foo VALUES(3, '2234567890');
INSERT INTO Foo VALUES(4, '2454567890');
INSERT INTO Foo VALUES(5, '9994567890');

SELECT keycol,
CASE
WHEN SUBSTRING(datacol, 1, 2) IN ('01', '02', '05') THEN 1
WHEN SUBSTRING(datacol, 1, 2) IN ('15', '16') THEN 2
WHEN SUBSTRING(datacol, 1, 2) IN ('18', '19', '22', '36')
OR SUBSTRING(datacol, 1, 3) IN ('174', '175', '177', '244',
'245', '297', '323', '334', '335')
THEN 3
ELSE 0
END AS col2
FROM Foo;

HTH,

Plamen Ratchevhttp://www.SQLStudio.com
Thank you Plamen, it works :-)
Jun 27 '08 #3

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

Similar topics

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...
2
by: cs168 | last post by:
Hi I am new in ASP programming so I do use the very basic and simple way to do all my stuff. Now I do really got stuck at how can I loop thru the calculation for all my selection.. My full code is as...
5
by: Iain Miller | last post by:
Trying to get my head round building a DB to analyse phone bills. On the surface its fairly simple - duration in seconds x cost per minute/60. The problem arises with working out what time of...
8
by: Bri | last post by:
Greetings, I am using Eval() in a query with only limited success. If the text within the function contains a reference to a Field I get #ERROR#. I'll try and explain what I'm trying to do and...
4
by: Anders | last post by:
Hi, I was wondering what is most efficient of the two. Is it more efficient to add server controls within the Itemtemplate and use OnItemDataBound to manipulate and databind the servercontrols. ...
3
by: Paul | last post by:
I have been trying to code a callback in vb.net using the delegate method. I have successfully compiled the program and run it but after it accessess the copyfileex winapi method and then calls the...
5
by: serge | last post by:
Is it generally or almost always better to have multiple small SPs and functions to return a result set instead of using a single big 1000+ lines SP? I have one SP for example that is 1000+...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
6
by: Mark | last post by:
Currently using MS Access 2000 and SQL Server Express. Using the current DAO OpenRecordset code listed below, however I keep getting the error message.... 3254: ODBC --Cannot lock all records ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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.