How to update the first non null column of each row? - sql-server-2008

I have following solution
Select Col1 = Case when Col1 is null then 'update'
else Col1
,Col2 = Case when Col1 is null then Col2
when Col2 is null then 'update'
else Col2
,Col3 = Case when Col2 is null then Col3
when Col3 is null then 'update'
else Col3
.... and so on
Just wondering if anyone has better solution.

Your solution is not correct.
SQL does not works this way, you need to check all preceding columns.
(And you forgot ENDs for your CASEs)
DECLARE #T TABLE(
col1 sysname NULL
,col2 sysname NULL
,col3 sysname NULL
)
INSERT INTO #T
SELECT NULL, 'N', NULL
-- Incorrect
Select Col1 = Case when Col1 is null then 'update'
else Col1
END
,Col2 = Case when Col1 is null then Col2
when Col2 is null then 'update'
else Col2
END
,Col3 = Case when Col2 is null then Col3
when Col3 is null then 'update'
else Col3
END
FROM #T
-- Dull, but correct
SELECT
Col1 = ISNULL(Col1, 'update')
,col2 = CASE
WHEN Col1 IS NOT NULL
AND Col2 IS NULL
THEN 'update'
ELSE Col2
END
,col3 = CASE
WHEN Col1 IS NOT NULL
AND Col2 IS NOT NULL
AND Col3 IS NULL
THEN 'update'
ELSE Col3
END
FROM #T

Related

How to handle null and NULL value in sql

So I have been facing this weird situation in mysql where somehow null values are inserted in my table.
I'm talking about null value not NULL value.
I have attached image for better understating
as you can see name column have null and mobile_no have NULL
So after using this query
select Case when t1.name IS NULL then 'NA'
when t1.name= 'NA' or 'null' or NULL then 'NA'
else t1.name end as 'Name',
Case when t1.mobile_no IS NULL then 'NA'
when t1.mobile_no= 'NA' or 'null' or NULL then 'NA'
else t1.mobile_noend as 'Mobile no' from student;
after this I'm getting this result
|Name|Mobile no|
----------------
|null|NA |
but I want below result
|Name|Mobile no|
----------------
|NA |NA |
To compare a column with multiple values use col IN (x, y, z), not col = x OR y OR z.
You also can't compare with NULL using = or IN, so that has to be a separate check.
select
Case
when t1.name IS NULL OR t1.name IN ('NA' or 'null') then 'NA'
else t1.name
end as 'Name'
Can you try use IN statement? That should work.
select Case WHEN (t1.name IN ('NA', 'null') OR t1.name IS NULL) THEN 'NA' else t1.name end as 'Name',
Case WHEN (t1.mobile IN ('NA', 'null') OR t1.mobile IS NULL) THEN 'NA' else t1.mobile_no end as 'Mobile no' from student;

How to get count of columns that are having null values for a given row in sql?

I have a table that are having 115 columns.
Out of 7 columns I need to get the count of columns that are having not null values for a given row.
One method is to use case and +:
select t.*,
( (case when col1 is not null then 1 else 0 end) +
(case when col2 is not null then 1 else 0 end) +
(case when col3 is not null then 1 else 0 end) +
(case when col4 is not null then 1 else 0 end) +
(case when col5 is not null then 1 else 0 end) +
(case when col6 is not null then 1 else 0 end) +
(case when col7 is not null then 1 else 0 end)
) as cnt_not_nulls_in_row
from t;
In MySQL, this can be simplified to:
select t.*,
( (col1 is not null ) +
(col2 is not null ) +
(col3 is not null ) +
(col4 is not null ) +
(col5 is not null ) +
(col6 is not null ) +
(col7 is not null )
) as cnt_not_nulls_in_row
from t;
You may first query the given row from the table using the primary key and the use COUNT to count the number of columns from the queried row having null value, as follows:
WITH derived_row as
(SELECT col1, col2, col3, col4, col5, col6, col7 FROM table WHERE primary_key=key)
SELECT COUNT(CASE
WHEN col1 IS NULL THEN 1
WHEN col2 IS NULL THEN 1
WHEN col3 IS NULL THEN 1
WHEN col4 IS NULL THEN 1
WHEN col5 IS NULL THEN 1
WHEN col6 IS NULL THEN 1
WHEN col7 IS NULL THEN 1
END) AS null_column_count
FROM derived_row;

Count no of column having null and non null value

Hi I have query in which i want to sum of null(0,'', NULL) and non null values in a single row.
Example: I have a table having 5 column. It contain atleast one record. In its first row 2 columns are blank and 3 columns are having some value. I want a query which will give me a result like non_null_count=3, null_count=2
Count of NOT NULL data-
SELECT Count(*)
FROM employee
WHERE salary IS NOT NULL
AND emp_name IS NOT NULL
AND manager_id IS NOT NULL
Count of NULL data-
SELECT Count(*)
FROM employee
WHERE salary IS NULL
AND emp_name IS NULL
AND manager_id IS NULL
You can use this.
SELECT ( IF(col1 IS NOT NULL, 1, 0)
+ IF(col2 IS NOT NULL, 1, 0)
+ IF(col3 IS NOT NULL, 1, 0) +... ) AS total_not_null,
( IF(col1 IS NULL, 1, 0)
+ IF(col2 IS NULL, 1, 0)
+ IF(col3 IS NULL, 1, 0) +... ) AS total_null
FROM mytable
In MySQL, boolean expressions can be treated as numbers, with "1" for true and "0" for false.
So, this does what you want:
select ((col1 is not null) + (col2 is not null) + (col3 is not null) +
(col4 is not null) + (col5 is not null)
) as num_not_null,
((col1 is null) + (col2 is null) + (col3 is null) +
(col4 is null) + (col5 is null)
) as num_null
from t;
Note that this interprets "blank" as being NULL. You can just as easily use <> '' or similar logic if "blank" means something else.
EDIT:
For other values, you need to extend the logic. A simple way is:
select ((col1 is not null and col1 not in ('0', '')) +
(col2 is not null and col2 not in ('0', '')) +
(col3 is not null and col3 not in ('0', '')) +
(col4 is not null and col4 not in ('0', '')) +
(col5 is not null and col5 not in ('0', ''))
) as num_not_null,
((col1 is null or col1 in ('0', '')) +
(col2 is null or col2 in ('0', '')) +
(col3 is null or col3 in ('0', '')) +
(col4 is null or col4 in ('0', '')) +
(col5 is null or col5 in ('0', ''))
) as num_null
from t;

How to Ignore Null Parameters in SQL Query

Well I have a problem, and I don't even know that what I'm trying or thinking to do is possible or not.
I have a sql query:-
select *
from table
where (col1 >= '2016-07-05' and col2 <= '2016-07-07')
and col3 = ''
and col4 = ''
and col5 = ''
and col6 = '686486'
and col7 = ''
and col8 = '';
Ok I'm using this query to perform a search operation, i want it to be very specific that's why i'm thinking of doing it this way.
All these parameters can be null or can have any value, So all i want is to ignore those paramters whose value is null.
For Eg:- For above query the result should be same as
select *
from vehicle
where (col1 >= '2016-07-05' and col2 <= '2016-07-07')
and col6 = '686486';
Edit 1: First of all thanks for helping me out,I tried what was suggested
select *
from table
where (('val1' is null or col1 >= 'val1') and ('val2' is null or col2 <=
'val2'))
and ('val3' is null or col3 = 'val3')
and ('val4' is null or col4 = 'val4')
and ('val5' is null or col5 = 'val5')
and ('val6' is null or col6 = 'val6')
and ('val7' is null or col7 = 'val7')
and ('val8' is null or col8 = 'val8');
But I'm getting 0 rows as a result for this query, Am I doing something wrong.
Like this
...
and (#value3 is null or col3 = #value3)
and (#value4 is null or col4 = #value4)
....

Mysql query with multiple conditions

Simply put : I have a table with
columns : col1 col2 col3 col4 col5 col6 col7
datas : val1 2 toto t f f f
val1 2 toto t t f f
val1 2 toto f t f t
val2 3 tata t f f t
val5 4 tutu f t f f
i want a query to give me this : val1 2 toto t t f t (the condition is where col2 = 2) = > the first 3 lines in one line with t when t is found in any of the last 4 columns
Is there any way to achieve this in one query ?
True and false are internally 1 and 0. Therefore a simple
SELECT
col1, col2, col3, max(col4),
max(col5), max(col6), max(col7),
sum(col6 + col7) as one_of_them_is_greater_0_meaning_true
from your_table
group by
col1, col2, col3
will do.
EDIT: Since you're storing varchars (bad idea), replace each true/false column with
CASE WHEN col = 't' THEN 1 ELSE 0 END /*of course replacing column name appropriatelly*/
EDIT 2:
SELECT
col1, col2, col3, max(CASE WHEN col4 = 't' THEN 1 ELSE 0 END),
max(CASE WHEN col5 = 't' THEN 1 ELSE 0 END), max(CASE WHEN col6 = 't' THEN 1 ELSE 0 END), max(CASE WHEN col7 = 't' THEN 1 ELSE 0 END),
sum(CASE WHEN col6 = 't' THEN 1 ELSE 0 END + CASE WHEN col7 = 't' THEN 1 ELSE 0 END) as one_of_them_is_greater_0_meaning_true
from Table1
where col2 = 2
group by
col1, col2, col3
see it working live in an sqlfiddle
1 is true, 0 is false. You can replace that again if you want to. And use alias names for columns of course. It's just a proof of concept.
Ok then you can use
SELECT field1, field2, field3,
MAX(IF(field4 = 't',1,0)) as is_any_field4_true,
MAX(IF(field5 = 't',1,0)) as is_any_field5_true,
MAX(IF(field6 = 't',1,0)) as is_any_field6_true,
MAX(IF(field7 = 't',1,0)) as is_any_field7_true
GROUP BY field1, field2, field3