I am trying to set a specific range of numbers for an IN clause in SQL Server for example, if the values of 1 - 100 are in column A, then do this...etc. My example below of what I am trying to do:
SELECT
CASE WHEN (values of 1 to 100) IN (columnA) THEN columnB
ELSE ...
END AS [column]
FROM table
Is this possible what I am trying to do within SQL Server?
Yes, the syntax would be:
SELECT
CASE WHEN columnA BETWEEN 1 AND 100 THEN columnB
ELSE ...
END AS [column]
FROM table
See the documentation on CASE statements here.
Related
I would like to use a SQL select statement that has the condition 'where column A is NULL change column B values to be equal to column C values'. How would I be able to incorporate this logic into a SELECT statement (Not an UPDATE statement as I cant change the tables on the server but want to query them from the server).
SELECT final.*
FROM final
The actual table is in the image below, here I want to change column Old to match column DirectUse if the Change column is null.
Try Case statement:
SELECT
Name, NameSimple, DirectUse, Year, Month,
CASE WHEN Change IS NULL THEN DirectUse ELSE Old END AS Old,
CurrentCons, Change
FROM final;
CASE: https://www.w3schools.com/sql/sql_case.asp
Can also be incorporated by UNION ALL:
SELECT Old
FROM final where Change is not null
UNION ALL
SELECT DirectUse
FROM final where Change is null
Use a CASE expression:
SELECT Name, NameSimple, DirectUse, Year, Month,
CASE WHEN Change IS NULL THEN DirectUse ELSE Old END AS Old,
CurrentCons, Change
FROM final;
I think you basically you want:
SELECT
ColumnA
, CASE WHEN ColumnA IS NULL THEN ColumnC ELSE ColumnB END AS ColumnB
, ColumnC
, <any other columns>
FROM Final
I have written a UDF function which will return a column (0 or 1) after processing 2 columns. I need to have my select query such that it returns those records for which this value is 1.
I wrote the query as below:
SELECT number, myUDF(col1, col2) as result
FROM mytable
WHERE result is not null
However it doesn't recognize the column name 'result'. Is there any special syntax needed so that it recognizes this new output column? Thanks.
CASE statement should solve the problem here:
SELECT number,
CASE when myUDF(col1, col2) = 1 then myUDF(col1, col2) END as result
FROM mytable
I need to use case when to generate the value range for in function (in DB2).
for example, in below code, I want the columnB in (5,6)
select columnA from tableName where columnB in (
(case
when #variable=1 then '4' // specific number
when #variable=2 then '5' //specific number
when #variable=3 then '7,10' // a value range
end)
)
but tried several times and other similar solutions, never got the expected result
how to do this?
Firstly, In function does not read multiple values inside Case statement. The comma must be after every single value in the range.
Second, you can mention a valid condition in your Question, rather than just 1=1. It's always true so, doesn't make sense.
Example:
1) output of below query gives in (5, 6)
select columnA from tableName where columnB in ((case when #variable=1 then 5 end), 6);
2) this gives only records of columnB = 5, let say the second condition is false.
select columnA from tableName where columnB in ((case when #variable=1 then 5 end), (case when #variable=2 then 6 end));
try Something like this
select columnA from tableName
where columnB in (
select * from table(values 4) tmp(NewCol)
where #variable=1
union all
select * from table(values 5) tmp(NewCol)
where #variable=2
union all
select * from table(values 7, 10) tmp(NewCol)
where #variable=3
)
You cannot have string as value range unless you convert it into rowset. I'm not sure how to do this in DB2, but I have something that should work, since according to documentation, DB2 does have unnest(). There are of course other ways to create rowsets.
SELECT columnA
FROM unnest(array[2,6,8,10], array[7,5,6,28]) --create "temp table" for example purposes
WITH ORDINALITY AS a(columnA, columnB) --alias columns from temp table
WHERE
CASE WHEN true THEN --switch true to some other condition
columnB IN(SELECT * FROM unnest(array[5,6])) --unnest(array[]) will create rowset with 2 rows, each having one column holding integer value
END;
You might need to drop alias from AS a(columnA, columnB) since I'm not sure if it works in DB2 and I have not found live DB2 tester (it is required in PostgreSQL where I tested query).
I am selecting into an existing column C from column A if it has a value greater than zero. If not then I want to populate it with column B. It will populate with column A only if column B is zero. If both column A and B have values it always uses B. Any help would be appreciated.
(
CASE
WHEN column A > 0 THEN column A
ELSE column B
END
)
My insert into was correct but my select had the columns reversed which was causing what I thought was an error. Thanks for all of your help. What a noob thing to do.
Your database of choice will be extremely useful, as it looks like you are already correct in most RDBMS. Just stick exactly what you have already inside of a select:
--Insert based on select
--INSERT INTO [TABLETOINSERT] (ColumnToInsert)
SELECT
CASE
WHEN ColumnA > 0 THEN ColumnA
ELSE ColumnB
END AS NewColumn
--Or create a table the select
--INTO [NEWTABLENAME]
FROM [TABLENAME]
Or update:
UPDATE [TABLENAME]
SET NewColumn =
CASE
WHEN ColumnA > 0 THEN ColumnA
ELSE ColumnB
END
Assuming you want to update the value permanently and not just change it during select, do:
update MyTable
set ColumnC = CASE
WHEN ColumnA > 0 THEN ColumnA
ELSE ColumnB
END
I am wondering whether is possible to assign a value to a casted column in SQL depending on real table values.
For Example:
select *, cast(null as number) as value from table1
where if(table1.id > 10 then value = 1) else value = 0
NOTE: I understand the above example is not completely Oracle, but, it is just a demonstration on what I want to accomplish in Oracle. Also, the above example can be done multiple ways due to its simplicity. My goal here is to verify if it is possible to accomplish the example using casted columns (columns not part of table1) and some sort of if/else.
Thanks,
Y_Y
select table1.*, (case when table1.id > 10 then 1 else 0 end) as value
from table1