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
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’m new to SQL so apologies if this is a basic question. I have a table in BigQuery that I’d like to add a new column to based on another.
The table has Column A with number values of FLOAT, I want to create Column B which will be False if Column A is less than 1.0 or True if it is greater. How exactly could I go about that?
Use if:
IF(columnA < 1, False, True)
TRY this
ALTER TABLE BigQuery
ADD B varchar(10) NULL
CONSTRAINT df_BigQuery DEFAULT
(CASE WHEN A < 1.00 THEN 'TRUE' else 'FALSE' END)
You can use below
select *, columnA < 1 as new_column
from your_table
Note: you do not need to use IF ... or CASE ... because columnA < 1 by itself already returns either true or false (with exception when columnA is null)
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.
I am wondering how i can display data from column B if Column A is null. The reason is if we get a product from one of our manufactures it is put in a different column. However, when i go to build the report the two columns essentially the same thing and it is throwing the graph way off. Any help would be appreciated.
Something like this maybe?
Case When column A isnull then column B?
Use ISNULL() or COALESCE(), or CASE
SELECT ISNULL(ColumnA, ColumnB) AS [YourColumn]
FROM FOO
OR
SELECT COALESCE(ColumnA, ColumnB) AS [YourColumn]
FROM FOO
OR
SELECT CASE WHEN ColumnA IS NULL THEN
ColumnB
ELSE
ColumnA
END AS [YourColumn]
FROM FOO
Consider a table A. Table A has a column named INNETTEDTXNID which is not the Primary key and neither unique and can be null.
I have a select * on table A based on some condition(not related to column INNETTEDTXNID).
For each of the rows fetched above i have to find a value called isNetted. The concept of isNetted is that if the column INNETTEDTXNID of this row is present more than once in the entire table(including this row) then the value of isNetted would be true for this row.
I hope my question is clear. Thanks for advance in help.
This will return the values of any entries that appear more than once.
SELECT a.INNETTEDTXNID
FROM TableA as a, TableA as b
WHERE
not a.id=b.id
and a.INNETTEDTXNID=b.INNETTEDTXNID
if your trying to do this specifically to check if a single number appears more than once you could do something like
SELECT COUNT(INNETTEDTXNID)
FROM TableA
WHERE INNETTEDTXNID='value'
if it returns one, then its unique, for more than one its not unique. IF you want it to return something like true or false you could use
SELECT
CASE WHEN (
SELECT COUNT(INNETTEDTXNID)
FROM TableA
WHERE INNETTEDTXNID='value'
)>1 THEN 'True'
ELSE 'False'
END
AS isNetted
Probably no-where near the most efficient but it works.
SELECT TableA.INNETTEDTXNID,
CASE
WHEN NetChk.INNETTEDTXNID IS NOT NULL Then 1
ELSE 0
END AS isNetted
FROM TABLEA
LEFT JOIN (
SELECT INNETTEDTXNID
FROM TableA
GROUP BY INNETTEDTXNID
HAVING COUNT(INNETTEDTXNID) >1)
NetChk ON TableA.INNETTEDTXNID = NetChk.INNETTEDTXNID