Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table customer with columns fname,lname, tipo, kode
I want a list all the customer with fname, tipo, approved
tipo is with populated with 'teacher' or 'student'
the third column should be taken from tipo and kode that menas if tipo is 'teacher' result ' ', if tipo is 'student and from the kode the 4-th and 5-th caracter are <10 return 'false' else return 'true'
how can I do this only with select without functions or stored procedures
SQL
select fname, tipo,

        (select  (case when SUBSTRING(kode, 4, 2) < 40  and tipo='student' then 'true'
                       when SUBSTRING(kode, 4, 2) <= 40 and tipo='student' then 'false'
                      else  ' '  end)  as approved from customer )
from customer
;

but returns me an error "
XML
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

"
thanks in advance
Posted
Updated 17-Nov-15 3:33am
v2

Why is your subquery also pulling from the same table and doing simple selects? You need to simplify this way down. Just think it through in your head, what all do you need from that customer table. Also, your logic is wrong. You have both < 40 and <= 40 so you'll have to fix that part on your own.

For example:
SQL
SELECT fname, tipo, CASE WHEN SUBSTRING(kode, 4, 2) < 40 AND tipo = 'student' THEN 'true'
 WHEN SUBSTRING (kode, 4, 2) <= 40 AND tipo = 'student' THEN 'false'
 ELSE ' ' END AS approved
FROM customer
 
Share this answer
 
Comments
Member 12146557 17-Nov-15 11:00am    
Yes you are right for both your suggestion, I change like that and again returns an error : Conversion failed when converting the varchar value 'H4' to data type int.

select fname, tipo, case when SUBSTRING(kode, 4, 2) < 40 and tipo='student' then 'true'
when SUBSTRING(kode, 4, 2) >= 40 and tipo='student' then 'false'
else ' ' end as approved
from customer
;

Thanks!
Richard Deeming 17-Nov-15 11:59am    
You're trying to convert a non-numeric substring to an integer. Change the order of your tests in the CASE statement, and check that the substring contains two numbers:

CASE
WHEN tipo != 'student' THEN ''
WHEN Substring(kode, 4, 2) Not Like '[0-9][0-9]' THEN ''
WHEN Convert(int, Substring(kode, 4, 2)) < 40 THEN 'false'
ELSE 'true'
END
Member 12146557 17-Nov-15 17:21pm    
thanks a lot! it works :)
ZurdoDev 17-Nov-15 19:47pm    
Glad to hear it. You're welcome.
George Jonsson 17-Nov-15 22:45pm    
Then you should accept the solution.
Check This...



SQL
SELECT fname,
       tipo,
       (SELECT ( CASE
                   WHEN Substring(kode, 4, 2) < 40
                        AND tipo = 'student' THEN 'true'
                   WHEN Substring(kode, 4, 2) <= 40
                        AND tipo = 'student' THEN 'false'
                   ELSE ' '
                 END ) AS approved
        FROM   customer A Where A.fname = B.fname And A.tipo = B.tipo)
FROM   customer B;
 
Share this answer
 
Comments
Member 12146557 18-Nov-15 6:37am    
The problem was that I didn't check that the substring contains two numbers.
I did in 2 ways, with the sugestion at first soluion and to avoid selecting in the same table I've done this:


SELECT fname, fname,
CASE
when SUBSTRING(kode, 4, 2) not like '[0-9][0-9]' then ' '
when convert(int,SUBSTRING(kode, 4, 2)) < 40 and TIPO='student' then 'true'
when convert(int,SUBSTRING(kode, 4, 2)) >= 40 and TIPO='student' then 'false'
else ' '
end as approved
FROM customer;


thanks for your answer :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900