I have table with 2 col :
UID NAME
-----------
111 AAA
222 BBB
Customer will enter the name and I have to retrieve UID with respective value. If name won't present in the rows, it has to retrieve 000, not like no rows.
I am trying to write query like this:
SELECT
CASE UID
WHEN Count(*) = 0 THEN '000'
ELSE UID
END
FROM table1
WHERE NAME ='XXX'
Please help me in this regard. Thanks in advance...
If UID is an integer, then you need to take casts into account:
select coalesce(cast(max(uid) as char(3)), '000')
from table1
where name = 'XXX'
The cast is intended to be to the type of UID, which seems to be char(3) in your example.
When there are no matching rows, then the max() function returns NULL. The coalesce() turns this into the value you are looking for.
try this
select case
when max(id) is null then
0
else
max(id)
end
from table1
where name = 'b'
You have error in case
SELECT case when count(UID) = 0 THEN '000' ELSE UID end FROM table1 where name = 'XXX'
sqlfiddle :http://www.sqlfiddle.com/#!2/257ea/1
Related
I have the following table
id
name
1
Gaurav
1
Ram
1
Gaurav
1
Gaurav
From the above table I want to fetch records if name have same value as Gaurav. For example one row has name Ram so it should not return any thing. If all value is Gaurav then return id.
On MySQL, you could use aggregation:
SELECT id
FROM yourTable
GROUP BY id
HAVING SUM(name = 'Gaurav') = COUNT(*);
On all databases:
SELECT id
FROM yourTable
GROUP BY id
HAVING COUNT(CASE WHEN name = 'Gaurav' THEN 1 END) = COUNT(*);
You can try this as well. Bit hardcoded. You can use 0 instead of null and remove where clause as well if you want.
SELECT Case When Name ='Gaurav' Then ID else NULL END AS ID
FROM Yourtable
where name ='Gaurav'
I have a column that gets generated using CAST(CASE WHEN A and B THEN SSS) as Name
Now I need another column which will use the value of the column above in the same way (CASE WHEN Name SSS THEN 123)
Is that possible?
UPDATE:
Example:
I have a table with
Name, Age, UserCode, UserId
I would like to generate a table using SELECT which includes
Name, UserCode, UserName
Name: returned from the column Name
UserCode:
CAST(CASE WHEN UserCode = 'A' and UserId = 'B' THEN 'SSS'
CASE WHEN UserCode = 'C' and UserId = 'Z' THEN 'ZAZ')
UserName: use the result from UserCode, so if UserCode is SSS then UserName become SAR123 etc...
Try this:
Select *,
CASE UserCode
When 'SSS' Then 'SAR123'
--When ... --<<Put other When clauses here
END AS UserName
FROM(
Select Name ,
CASE WHEN UserCode = 'A' and UserId = 'B' THEN 'SSS'
WHEN UserCode = 'C' and UserId = 'Z' THEN 'ZAZ'
END AS UserCode
From Table
) AS K
The alias of column name are obtained after the FROM, WHERE and then SELECT clause code evaluation .. so these alias are not available before this evalution is finished for this reason
alias of column name are not allowed in select or where clause and for these clause you must rewrite the code when need
select (CASE WHEN CAST(CASE WHEN A and B THEN SSS .....) SSS THEN 123 .... )
FROM my_table
.......
but alias for column name are available for ORDER BY and GROUP BY (clause that are evalueted after the previous mentioned)
alias of coulmn can be used in order by and group by
I am not sure what I am trying is achievable or not!!
I am trying to write a SQL query will will do select statement based on user input.
so if user input = 1 then I want it to select from actual table.
if user input = 0 then I want it do select 0 or null from dual. (if this is possible).
so Here is Parameter which will used to get input from user. ?i_userkey:'':null?
if user input's 1 then it will change null to 1.
I want to write a query using this parameter. something like this.
below is the logic.
IF i_userkey = 1 then
select ID,Gender,Age from TableA
If i_userkey = 0 then
select 0 or null from dual.
is this possible?
How about this:
SELECT
CASE WHEN i_userkey = 1 THEN ID ELSE NULL END AS ID
CASE WHEN i_userkey = 1 THEN Gender ELSE NULL END AS Gender
CASE WHEN i_userkey = 1 THEN AGE ELSE NULL END AS Age
FROM TableA
This will at least give you a consistent three-column result set you can work with. Having the query return differing column counts is not going to work.
select ID,Gender,Age
from TableA
where i_userkey = 1
union all
select 0, 0, 0
from dual
where i_userkey = 0
You might have to adjust the datatypes in the dual-select to match TableA
I am trying to get my result as either 1 or 0 based on if the column note is empty or not
column is a varchar and i want the result from the query to give me a boolean value for that column note
SELECT id, product, note FROM myTABLE
what i tired is,
SELECT id, product, CASE WHEN ISNULL(note) THEN 0 ELSE 1 FROM myTable
but i am getting an error... i am sure there is simple fix to this i am missing so please help. thank you.
Try
SELECT id, product,CASE WHEN note IS NULL THEN 0 ELSE 1 END as SomeColumn
FROM myTable
to check for blank and null as well, use NULLIF
SELECT id, product,
CASE WHEN NULLIF(note,'') IS NULL THEN 0 ELSE 1 END as SomeColumn
FROM myTable
inside an SQL SELECT CASE STATEMENT how would i check if the a value for some coulmn in sometable is equal to 0 where the id is equal to something?
select
case
when (select column_name from sometable where id = #id) = 0 then
else
end
select
case when( select column from table where id=333)=0 then "your condition"
else
end
My interpretation of your question:
SQL check if numerical value is 0 for a field (Field1) [only] when id is=333
[If id is not 333, don't need to check]
SELECT CASE WHEN isnull(id,0) <> 333 OR Field1=0 THEN 1 ELSE 0 END
,other1, other2
FROM tbl