SQL: What does NULL as ColumnName imply - sql

I understand that AS is used to create an alias. Therefore, it makes sense to have one long name aliased as a shorter one. However, I am seeing a SQL query NULL as ColumnName
What does this imply?
SELECT *, NULL as aColumn

Aliasing can be used in a number of ways, not just to shorten a long column name.
In this case, your example means you're returning a column that always contains NULL, and it's alias/column name is aColumn.
Aliasing can also be used when you're using computed values, such as Column1 + Column2 AS Column3.

When unioning or joining datasets using a 'Null AS [ColumnA] is a quick way to make sure create a complete dataset that can then be updated later and a new column does not need to be created in any of the source tables.

In the statement result we have a column that has all NULL values. We can refer to that column using alias.
In your case the query selects all records from table, and each result record has additional column containing only NULL values. If we want to refer to this result set and to additional column in other place in the future, we should use alias.

It means that "aColumn" has only Null values. This column could be updated with actual values later but it's an empty one when selected.
---I'm not sure if you know about SSIS, but this mechanism is useful with SSIS to add variable value to the "empty" column.

When using SELECT you can pass a value to the column directly.
So something like :
SELECT ID, Name, 'None' AS Hobbies, 0 AS NumberOfPets, NULL AS Picture, '' AS Adress
Is valid.
It can be used to format nicely a query output when using UNION/UNION ALL.

Query result can have a new column that has all NULL values. In SQL Server we can do it like this
SELECT *, CAST(NULL AS <data-type>) AS as aColumn
e.g.
SELECT *, CAST(NULL AS BIGINT) AS as aColumn

How about without using the the as
SELECT ID
, Name
, 'None' AS Hobbies
, 0 AS NumberOfPets
, NULL Picture

Usually adding NULL as [Column] name at the end of a select all is used when inserting into another table a calculated column based on the table you have just selected.
UPDATE #TempTable SET aColumn = Column1 + Column2 WHERE ...
Then exporting or saving the results to another table.

Related

How do I find empty values in multiple columns at once using SQL Big Query?

To find for one column, I can use this.
SELECT column1
FROM `dataset.table`
WHERE column1 IS NULL OR column1 = '';
But what if i have 100 columns? Instead of going through column by column, changing column 1 to 2,3,etc.,I'm looking for one for all solution. I'm kinda new to SQL and Data Cleaning.
Consider below approach
select *
from your_table t
where regexp_contains(to_json_string(t), r':(?:null|"")[,}]')
above will return you all rows where any column either null or empty string

Trying to create cleaner sql syntax

Here is what I am trying to do:
select * from table
where in ('completedDate', 'completedBy', 'cancelDate', 'cancelBy') is not null
If the four columns above are not null I need to display the records. I know I would do this with several where/and clauses but trying to learn and make my new stuff cleaner.
Is what I am trying to do above possible in a cleaner way?
If I understand correctly I guess you want to do that:
select *
from table
where completedDate is not null
and completedBy is not null
and cancelDate is not null
and cancelBy is not null
Regarding clarity of code I don't see a better way to write it, that's what I would code anyway.
EDIT: I wouldn't really do that in this case, but if this is a very common condition you can add a computed column in the table (stored or not), or create a view on top of table, and do:
select * from view where importantFieldsAreNotNull = 1
If I understand correctly, you want to return records where all four columns are not null?
The standard and (in my opinion) most readable way to do this would be:
Select
*
From
YourTable
Where
Column1 IS NOT NULL AND
Column2 IS NOT NULL AND
Column3 IS NOT NULL AND
Column4 IS NOT NULL;
To Check if all Columns are not null:
select * from table
where completedDate is not null
and completedBy is not null
and cancelDate is not null
and cancelBy is not null
You could use the COALESCE function to determine if all the column values were NULL.
The COALESCE function takes between 1 or more arguments and returns the first non-null argument. If at least one of the arguments passed into COALESCE is NOT NULL, then it will return that value, otherwise if all the arguments are NULL it returns NULL.
SELECT *
FROM TABLE
WHERE COALESCE(Column1, Column2, Column3, Column4) IS NOT NULL
Also depending on the datatypes of the columns, you may have to CAST them to the same datatype. For example, I wasn't able to use the COALECSE function on a DateTime column and a CHAR column without casting.
However, even though this would be shorter, I would not consider it "cleaner". I'd think it would be harder to read and maintain compared to having multiple ANDs in the WHERE clause.
-- Under reasonable assumption on data types:
select *
from [table]
where completedBy+cancelBy+DATENAME(yy,completedDate)+ DATENAME(yy,cancelDate)
is not null

how to filter in sql script to not include any column null

imagine there are 50 columns. I dont wan't any row that includes a null value. Are there any tricky way?
SQL 2005 server
Sorry, not really. All 50 columns have to be checked in one form or another.
Column1 IS NOT NULL AND ... AND Column50 IS NOT NULL
Of course, under these conditions why not disallow NULLs in the first place by having NOT NULL in the table definition
If it's SQL Server 2005+ you can do something like:
SELECT fields
FROM MyTable
WHERE stuff
EXCEPT -- This excludes the below results
SELECT fields
FROM MyTable
WHERE (Col1 + Col2 + Col3....) IS NULL
Adding a null to a value results in a null, so the sum of all your columns will be NULL.
This may need to change based on your data types, but adding NULL to either a char/varchar or a number will result in another NULL.
If you are looking at the values not being null, you can do this in the select statement.
SELECT ISNULL(firstname,''), ISNULL(lastname,'') FROM TABLE WHERE SOMETHING=1
This will replace nulls with string blanks. If you want another value use: ISNULL(firstname,'empty') for example. You can use anything where the word empty is.
I prefer this query
select *
from table
where column1>''
and column2>''
and (column3>'' or column3<'')
Allows sql server to use an index seek if the proper index/es exist. you would have to do the syntext for column 3 for any numeric values that could be negative.

How should I deal with null parameters in a PL/SQL stored procedure when I want to use them in comparisons?

I have a stored procedure with a parameter name which I want to use in a where clause to match the value of a column i.e. something like
where col1 = name
Now of course this fails to match null to null because of the way null works. Do I need to do
where ((name is null and col1 is null) or col1 = name)
in situations like this or is there a more concise way of doing it?
You can use decode function in the following fashion:
where decode(col1, name, 0) is not null
Cite from SQL reference:
In a DECODE function, Oracle considers
two nulls to be equivalent.
I think your own suggestion is the best way to do it.
What you have done is correct. There is a more concise way, but it isn't really better:
where nvl(col1,'xx') = nvl(name,'xx')
The trouble is, you have to make sure that the value you use for nulls ('xx' is my example) couldn't actually be a real value in the data.
If col1 is indexed, it would be best (performance-wise) to split the query in two:
SELECT *
FROM mytable
WHERE col1 = name
UNION ALL
SELECT *
FROM mytable
WHERE name IS NULL AND col1 IS NULL
This way, Oracle can optimize both queries independently, so the first or second part won't be actually executed depending on the name passed being NULL or not.
Oracle, though, does not index NULL values of fields, so searching for a NULL value will always result in a full table scan.
If your table is large, holds few NULL values and you search for them frequently, you can create a function-based index:
CREATE INDEX ix_mytable_col1__null ON mytable (CASE WHEN col1 IS NULL THEN 1 END)
and use it in a query:
SELECT *
FROM mytable
WHERE col1 = name
UNION ALL
SELECT *
FROM mytable
WHERE CASE WHEN col1 IS NULL THEN 1 END = CASE WHEN name IS NULL THEN 1 END
Keep it the way you have it. It's more intuitive, less buggy, works in any database, and is faster. The concise way is not always the best. See (PLSQL) What is the simplest expression to test for a changed value in an Oracle on-update trigger?
SELECT * FROM table
WHERE paramater IS NULL OR column = parameter;

SQL set column names from another query

I'm am using MS Reporting Services to graph some data from an Oracle database. I want to name the columns in my select statement with values from another select statement. Is this possible?
Like instead of
Select Column1 As 'Test' From Table1
could I do something like
Select Column1 As (Select column2 from Table2 where Value = 1) From Table1
?
I would think you'd have to query out separately, then form the query dynamically. Interested to see if there is a different answer.
My PL/SQL's a little rusty, so what follows is more pseudocode than compilable & tested code. And this is completely off the top of my head. But if you know the specific ordinal location of the column in the table, you may try this:
columnName varchar2(50) :=
Select column_name
From all_tab_columns c
Where lower(table_name) = '<% Your Table2 Name %>' And
column_id = 9 -- The appropriate ordinal
Order By column_id;
Select Column1 As columnName From Table1;
There may be more column values drawn from "all_tab_columns" that'll help you as well. Take a look around and see.
I hope this helps.
You can query all needed column names into separate report dataset, create hidden multivalue report parameter vColumns, set dataset with columns as a parameter default values, and use it as a string array:
Parameters!vColumns(0).Value - will be the first column etc. So you can use them as a query parameters.
See Lesson 4: Adding a Multivalue Parameter