Sql: How to properly check if a record exists that matches 3 column - sql

I have read through the forum but i could not find any example or answer for check existing record for multiple column.
Question:
To check if an entry exist if match 3 column. There is 9 column in the table if 2 column entry match are not counted as existing record.

SELECT COUNT(id) AS existing_row_count FROM tablename WHERE col1 = ? AND col2 = ? AND col3 = ?
If existing_row_count returns greater than zero then we can say that there is at least one match. You can try this also:
SELECT id FROM tablename WHERE col1 = ? AND col2 = ? AND col3 = ?
Thus, you can get the matching row id numbers. And if we combine those two queries:
SELECT id, COUNT(id) AS existing_row_count FROM tablename WHERE col1 = ? AND col2 = ? AND col3 = ?

Related

How to fill values in condition in sql

I have tables like following
I'd like to fill col2in9999/12/31 by referring to col1==A
col1 col2
A 2011/1/1
B 2013/4/1
C 2000/1/1
A 2010/1/1
Therefore my desired result is following
Are there any way to achieve this?
col1 col2
A 9999/12/31
B 2013/4/1
C 2000/1/1
A 9999/12/31
If someone has opinion,please let me know
Thanks
If you have one table and that's all that needs updating then:
UPDATE [table] SET col2 = '31-DEC-9999' WHERE col1 = 'A';
If they're two different tables you could just run CREATE TABLE table2 AS SELECT * FROM table1 and then just run that UPDATE above ^. Sure it's an extra step but it does the job.
Simply add case statement to your query:
SELECT COL1, CASE WHEN COL1 = 'A' THEN '9999/12/31' ELSE COL2 END AS COL2 FROM TABLE1;

Rename category in the column in SQL Server

Here is the query
select col1
from table
col1 contains these category values:
A
B
C
NULL
How can I rename null category to D?
If you want to make the change permanent
UPDATE table
SET col1 = 'D'
WHERE col1 IS NULL
From then on you can simply query with ...
SELECT col1
FROM table
... to get the desired result.
If there is more than one row having a NULL in col1, you need to filter by a unique key, preferably by the primary key (which every table should have by the way). Let's say you have a table like
id (PK) col1
--- ----
1 'A'
2 'B'
3 'C'
4 NULL
5 NULL
then you can fix it with
UPDATE table SET col1 = 'D' WHERE id = 4;
UPDATE table SET col1 = 'E' WHERE id = 5;
unless you can calculate the new value from another column, e.g.:
UPDATE table
SET col1 = UPPER(LEFT(name, 1))
Try this : ISNULL( ) function is used to replace NULL value with another value
select isnull(col1,'D') as col1
from table
SQL Server uses ISNULL().
SELECT ISNULL(value_to_check, use_this_instead_if_valuetocheck_is_null)
For your code:
select ISNULL(col1, 'D') AS col_name
from table
However, this will happen across the board for this column. You can't use this to make a sequence, like D then E then F. Any NULL value you come across in this column will change to D.

Select where column in not null array

I am trying to do :
SELECT *
FROM table
WHERE column IN (SELECT col FROM table2 WHERE col2 = value )
but I want to check if the second request doesn't return a null array.
How is that possible?
Thanks in advance
Simply add a NOT NULL check in the subquery to omit the null values returned.
SELECT * FROM table WHERE column IN
(SELECT col FROM table2 WHERE col2 = value AND col IS NOT NULL);

sql query to return single value based on column it matches

I've a requirement where in if the query string matches column1 , return me 1. If it matches column 2 return 2 else if it matches column 3 return 3.
Table strunctre:
col1 col2 col3
11 12 13
22 23 24
If my query string is 23, then i'm expecting a return value of 2 as it matches col2.
Something like below:
select 1
from table1
where col1=querystring
and orderid=xxx
or select 2 from table1
where col2=querystring
and orderid=xxx
or select 3 from table1
where col3=querystring and orderid=xxx
Basically i'm expecting one query which return single value based on the column it matches.
Is it something doable in SQL as i'm not very good in DB skills.
Any help is highly appreciated.
There are a couple of approaches. If there is a guarantee that no more than one column will match at a time, a UNION will work:
SELECT 1 AS SomeCol
FROM table1
WHERE col1 = querystring
AND orderid = xxx
UNION
SELECT 2
FROM table1
WHERE col2 = querystring
AND orderid = xxx
UNION
SELECT 3
FROM table1
WHERE col3 = querystring
AND orderid = xxx;
If more than one match can happen, another approach is this (note the order of precedence is now col1, col2, col3 etc):
SELECT CASE
WHEN col1 = querystring THEN 1
WHEN col2 = querystring THEN 2
WHEN col3 = querystring THEN 3
END AS SomeCol
FROM table1
WHERE orderid = xxx;
Please try using case
declare #var int
set #var=23
select
case #var when col1 then 1
when col2 then 2
when col3 then 3 end
from YourTable
where
col1=#var OR
col2=#var OR
col3=#var
Try using IF-ELSE condition in your query and check with passed parameter.

How to choose a columns of a select statement based on conditions?

I have to create an SQL Server 2005 query which checks for the value of one attribute in the table and based on its value, select different sets of columns.
How can I do that?
for e.g.
In table 'car', if the values of 'type' attribute are 1 and 2
when type = 1, i want to execute a select 'query1' with 3 columns.
when type = 2, i want to execute another select 'query2' with 4 other columns.
How do I do that?
Please help.
I think you're looking at a Stored Procedure with an If statement. CASE will work, but it can't change the number of columns returned.
SELECT
Col1 = CASE WHEN Type = 1 THEN (SELECT Null FROM T1)
ELSE (SELECT Col1 FROM T2) END
, Col2 = CASE WHEN Type = 1 THEN (SELECT Col1 FROM T1)
ELSE (SELECT Col2 FROM T2) END
, Col3 = CASE WHEN Type = 1 THEN (SELECT Col2 FROM T1)
ELSE (SELECT Col4 FROM T2) END
, Col4 = CASE WHEN Type = 1 THEN (SELECT Col3 FROM T1)
ELSE (SELECT Col4 FROM T2) END
FROM Cars
If you would show us the DDL of all tables involved, you'd probably get a better answer or a different (read better) approach.