In my table 1,00,000 records are there, where 1000 rows and 100 columns are there, some of the records in the table have NULL values, what i want is If the Null valued recorded of entire rows will come to first of my table it can be easy to me to check which row contains and which column contains NULL values
- If the Value of ROW/COLUM=NULL print all the null valued Rows and columns First
- Non-Null valued columns print next of the NULL valued rows
My Table is like this:
Staff_ID First_Name Last_Name
1 Stephen Jiang
2 Ravi chaganti
3 Michael Blythe
4 NULL NULL
5 Jillian NULL
6 NULL Vargas
and I want output like this:
Staff_ID First_Name Last_Name
4 NULL NULL
5 Jillian NULL
6 NULL Vargas
1 Stephen Jiang
2 Ravi chaganti
3 Michael Blythe
I need a query for this.
Also try this:
Query with order by will be challenging so Here it is:
NULL
values will be ordered first.When concatenating a value with
NULL
the result isNULL
soFirst_Name+Last_Name
will beNULL
if any one isNULL
.cast(Staff_ID as varchar(10))+...
is there guarantee the order you want for the case whenFirst_Name+Last_Name
is notNULL
(I assumeStaff_ID
isint
).And the extra order by column
Staff_ID
is there to guarantee the order you want for all the rows whereFirst_Name+Last_Name
isNULL
Update
You can build the query dynamically using INFORMATION_SCHEMA.COLUMNS. This might do what you want. Note, this is not tested for all kinds of data types.
Update 2
A non dynamic version using XML instead.
Update 3
If you know the primary key of your table and don't mind typing the where clause you can use this instead of using
row_number()
.select Staff_ID, First_Name, Last_Name FROM [tablename]
would get you the output you described ;P nothing in particular needed to print out the null values.If you want to look for specific null values use WHERE columnname IS NULL
Edit: if you are just looking to sort by null first just do a ORDER BY Last_Name, First_Name ASC/DESC depending on what way you want to sort it.