Conditionally select value of a column based on another column - sql

I have a query that at high level is something like this:
Select SomeColumns, LastName, StartDateString
from MyTable
Is it possible to have a logic in this select statement that can say print for value of "StartDateString" column the "not started" if LastName is for example "abcd". and print its real value otherwise.
Basically I want to know how to write a logic for output of one column based on value of some other column in the same row. ( I am using SQL Server )

You can use CASE.
SELECT
SomeColumns,
LastName,
CASE WHEN LastName = 'abcd' THEN 'Not Started'
ELSE StartDateString END AS StartDateString
FROM MyTable

Select SomeColumns, LastName, StartDateString ,(case when Lastname = 'abcd'+
then 'not started' else 'started' +'on' + cast (StartDateString as varchar) end) as NewColumn
from MyTable

Related

Is it possible to use a SQL query as a result in CASE in ORACLE SQL?

For example something like this:
SELECT CASE
WHEN COL_1 LIKE '%4'
THEN (SELECT MAX(COL_1) FROM TAB_1)
ELSE MAX(COL_1)
END AS "RESULT"
FROM TAB_1
Yes this is fine. A query in this context is called a scalar subquery. It needs to return one column (in general) and at most one row.
This particular version, though, has problems:
SELECT (CASE WHEN COL_1 LIKE '%4'
THEN (SELECT MAX(COL_1) FROM TAB_1)
ELSE MAX(COL_1)
END) AS "RESULT"
FROM TAB_1
In particular, for the CASE WHEN to work, then COL_1 needs to be in the GROUP BY. However, it would be strange to then be taking the maximum.
I suspect you intend two columns or two rows:
SELECT MAX(CASE WHEN COL_1 LIKE '%4' THEN COL_1 END) as MAX_4
MAX(CASE WHEN COL_1 LIKE '%4' THEN NULL ELSE COL_1 END) as MAX_not4
FROM Ttabl_1
Yes it is possible to do that, here is an example for that
Lets assume following 2 tables
Table 1: CITY (ID, NAME, COUNTRYCODE)
Table 2: COUNTRY (CODE, NAME, CONTINENT, REGION)
For the above tables you can use the query as
select id,
case when countrycode like 'AUS'
then (select name from country where code like 'AUS')
else countrycode end
from city;

SQL if/case statement - returning value from db

I have trouble with sql command in MS Sql server .....
I would like to check names in table. in Column are e.g. Carl, Loius, Manfred, Jenny ....
Is there any way, to create case or if statement :
Case when name = 'Carl' then 'Man'
else Return original value(name) from table?
(Louis, Manfred, Jenny) ?
If yes, how can i achieve that ? Case statement? If statement?
Thanks in advance
You can do:
select (Case when name = 'Carl' then 'Man' else name end)
you mean something like this ?
select name,
case when name = 'Carl' then 'Man'
when name = 'Jenny' then 'women'
else name end
from yourtable
Select case when name in ('Carl','Manfred',....)
Then 'Man' else 'Women' end as gender
from your_table
Another (shorter to write) option is to use nullif and isnull:
SELECT ISNULL(NULLIF(name, 'Carl'), name)) as name
FROM yourTable.
The NULLIF function will return null if both arguments are equale, and the ISNULL will return the first argument unless it's null - in that case it will return the second argument.

CASE SQL Oracle Query - What is wrong?

I have following query to select the view to be used in the query, however I get the error:
FROM keyword not found where expected
select *, (CASE WHEN 'employee' = 'employee' THEN employee ELSE vw END) FROM type1
select type1.*,
(CASE WHEN 'employee' = 'employee'
THEN employee
ELSE vw
END)
FROM type1
I always prefix with the tablename/table alias to the * and it works!!
We just need to specify, fetch all the column from this table, when we specify combination of wildcard and other select expressions!
You cannot use * and individual columns together in select statement.
SELECT (CASE WHEN 'employee' = 'employee' THEN 'employee' ELSE 'vw' END)
FROM dual
You cant do a *, in Oracle without an alias/TableName(.) in front of it. List out the columns you want to see in the result set or add a table alias/tablename.
You cannot set the name of the view the select statement shall retrieve data from in the column list part of the statement.

case statement in select query in sql

I have a stored procedure to load the data from one table to another table.
i need to set the column value of the destination table based on the two values of the select statement, some thing like the below example.
insert into table table_name
( value1, value 2,value 3)
select (value 1,value2 ,
case value3
when value1 = 'somevalue' &&* value2 = 'somevalue'
then 'x'
else 'y'
End
from table_name.
can any one help me to find out how to update the a column in based on the two previous column values in the same select query?
i have tried with the below sample example to understand but it was failed to parse.
INSERT INTO HumanResources.departmentcopy
( DepartmentID,GroupName,Name,temp)
SELECT DepartmentID,GroupName,Name,
CASE temp
WHEN DepartmentID = 1 && Name = 'Engineering and Research'
THEN 'sucessful'
ELSE 'unsucessful'
END
FROM HumanResources.department
Help me on this!!
thanks,
Venkat
You were very close:
INSERT INTO HumanResources.departmentcopy(DepartmentID, GroupName, Name, temp)
SELECT DepartmentID,
GroupName,
Name,
CASE WHEN DepartmentID = 1 AND Name = 'Engineering and Research'
THEN 'sucessful' ELSE 'unsucessful' END
FROM HumanResources.department
&& is not valid in SQL. Use AND to append a condition.
INSERT INTO HumanResources.departmentcopy( DepartmentID,GroupName,Name,temp)
SELECT DepartmentID,
GroupName,
Name,
CASE
WHEN DepartmentID = 1 AND Name = 'Engineering and Research' THEN 'sucessful'
ELSE 'unsucessful'
END
FROM HumanResources.department
INSERT INTO HumanResources.department(DepartmentID, GroupName, Name, temp)
SELECT DepartmentID,
GroupName,
Name,
CASE WHEN DepartmentID = 1 AND Name = 'Engineering and Research'
THEN 'sucessful' ELSE 'unsucessful' END
FROM HumanResources.department

How to summarize SQL table to return value conditionally

I have a table with several rows, and several columns. It looks like this:
Name Description
X PASS
X PASS
X FAIL
I want it to return only one row. If all of them are PASS, return PASS.
If one or more of them are FAIL, then return FAIL.
What's the best way to go about achieving this in SQL Server 2008?
EDIT: The values in the name column will always be the same.
Depending on the database indexes, and assuming you want one row returned per unique name, I would look at the performance of
select
name,
min([description]) as description
from
tableA
group by
name
compared to the other solutions
SELECT TOP 1 CASE Description WHEN 'FAIL' THEN 'FAIL' ELSE 'PASS' END
FROM DaTable
ORDER BY Description
OP: Is it possible that the table is empty? In that case this query won't return any rows, obviously.
EDIT
According to aquinas's comment I created a modified query without ordering:
SELECT CASE COUNT(Description) WHEN 0 THEN 'FAIL' ELSE 'PASS' END
FROM DaTable
WHERE Description = 'FAIL'
This query will return PASS if DaTable is empty.
This is the simplest solution you will find:
SELECT MIN(Description) FROM tbl
If there's at least one FAIL, then our result column will contain FAIL, otherwise, it will contain PASS.
You can use EXISTS to get the existance of a row containing "FAIL".
You could also try something like:
SELECT TOP 1 COALESCE(tFail.Description,t.Description)
FROM myTable AS t
LEFT JOIN myTable AS tFail ON tFail.Name = t.Name AND tFail.Description = 'FAIL'
WHERE t.Name = 'x'
Here is the query:
--DROP TABLE result
CREATE TABLE result(Name varchar(10),Description varchar(20))
--select * from result
INSERT INTO result
VALUES('X','PASS'),('X','PASS'),('X','FAIL')
;WITH CTE(descp,cnt) as (SELECT [description],COUNT(*) as cnt FROM result group by [description])
SELECT CASE WHEN COUNT(*) > 1 then 'FAIL' when COUNT(*)=1 then MAX(descp) else 'PASS' END FROM CTE