Only want a Concatenation to appear when the value is not null - sql

I have built a concatenation using SQL (Oracle), but I only want the concatenation to output when the value in the field is not null. I'm effectively building a website URL in the field, but in some cases the link is not yet available, but the concatenation still outputs the prefix (http://www.). If the value is null, then it should output null. At the moment I have:
SELECT 'http://www.'||LINK AS "URL"
FROM TABLE

If selecting only rows from TABLE where LINK IS NOT NULL isn't an option, you can use NVL2() for this. It accepts three arguments - a string, the value to return if the string is not null, and the value to return if the string is null.
SELECT NVL2(LINK, 'http://www.'||LINK, NULL) AS "URL" FROM TABLE;

You could use NVL2 as the other answer suggested. Or alternatively do something like -
SELECT CASE WHEN LINK IS NOT NULL THEN
'http://www.' || LINK
ELSE
NULL
END
AS "URL"
FROM TABLE;

I would go even further. You have Oracle so you have regular expressions at your disposal (or you do if you have 9i or greater), so you can check to see if your link already starts with http://:
SELECT CASE WHEN REGEXP_LIKE(link, '^https?:\/\/') THEN link
WHEN link IS NOT NULL THEN 'http://www.' || link END AS url
FROM mytable;
The CASE statement will return NULL if there is no ELSE clause, so you need not add an explicit case for link IS NULL. Personally, I would go so far as to make sure that link didn't start with www. as well, or if it even should.

Related

Cast string as "Column Reference" in HIVE SQL

I have a query that is meant to use bind values to retrieve information from a table and test if any field is NULL. The user enters a column name for the bind value and that column is then tested for any NULL values. Here is a simplified version of the query:
SELECT
CASE
WHEN ISNULL(bind.value)
THEN 'PASS'
ELSE 'FAIL'
END AS Solution
This keeps returning 'FAIL' I think because ISNULL() is testing the column entered as a string. Instead, I need it to test the fields in the column, rather than the string holding the column's name. Is there anyway to cast this string as a reference or pointer (I know SQL doesn't have pointer but a pointer-like object) to the a column?
NOTE: When I replace bind.value with the column name it returns 'PASS.' I'm really trying keep this as dynamic as possible so can utilize it with other tables without having to write a new query for each table I use this on.
Most probably you are passing empty strings instead of NULL.
empty string '' is not the same as NULL in Hive, add test for empty string:
SELECT
CASE
WHEN ISNULL(bind.value) OR cast(bind.value as string)='' THEN 'PASS'
ELSE 'FAIL'
END AS Solution

Oracle:Difference between NULL and EMPTY string

I am in a situation where my query is not returning any values due to oracle behaviour.
Problem is this:Oracle considers EMPTY STRING as NULL when INSERTING DATA but not when SELECTING BACK the data.
This is not a duplicate of Why does Oracle 9i treat an empty string as NULL? because here i am not asking for the reason to this problem,i am well aware of the reason,i am asking for a solution to this problem.
This is my table structure
CREATE TABLE TEST
(
ID NUMBER not null,
NAME VARCHAR2(255)
)
when inserting the values oracle will accept
INSERT INTO TEST values(1,'');
I found out that internally oracle converts Strings of Zero length to NULL and stores
But my query
SELECT * FROM TEST
WHERE NAME = INPUT;(INPUT='')
(Input is passed from front end and will sometimes have empty string)
will not return any result
I can not write dynamic query due to performance issue
Somebody who faced this issue before please let me know how do i compare EMPTY STRING with NULL
The problem is that Oracle (by default) treats empty strings as NULL. Hence:
where name = ''
is the same as:
where name = NULL
and both always fail (because they return NULL).
You can fix this in various ways. One method is:
where (name = INPUT or name is null and INPUT is null)
Or, if you know there is an invalid name:
where coalesce(name, '<invalid>') = coalesce(INPUT, '<invalid>')
This is one of the most annoying features of Oracle - not found in other DB products. You will have to put up with it, for all the other massive advantages of Oracle - and be prepared that the learning curve is not very quick.
To check for equality of nulls, the best approach is to write explicitly what you are doing, instead of using gimmicks. For example:
... where NAME = INPUT or (NAME IS NULL and INPUT IS NULL)
This will make it a lot easier for yourself, and for others after you, to debug, maintain, and modify the code, now and especially later. There are other solutions, too, but they may confuse others in the future; for example, this is something I wouldn't use (for several reasons):
... where NAME || 'z' = INPUT || 'z'
although it would obviously achieve the same result with less typing.
One more thing, in most cases you should NOT include in your results rows where you treat NULL as "equal" - the values are NULL for a reason, and in most cases if you make two NULL's equal, that is NOT the intended result.

SQL WHERE is anything

I'm working on a database query via a search bar and would like it to sometimes yield all results (depending on what is inputted)
I know that for SELECT you can use * in order to select all columns. Is there similar SQL syntax: i.e. WHERE name IS * to essentially always be true?
Edit to clarify:
The nature of the clause is that a variable is used to set the name (I'm actually not able to change the clause, that was made clear). i.e. WHERE name IS [[inputName]] (inputName is the decided by the search bar)
WHERE ISNULL(name, '') = ISNULL(name, '')
(assuming that 'name' is of a string type)
Just make the column reference itself. However, if this is the only goal of your query, why are you against omitting the WHERE clause?
If you want to return all results in a SQL statement, you can simply omit the WHERE clause:
SELECT <* or field names> FROM <table>;
You should use WHERE only when you want to filter your data on a certain field. In your case you just don't want to filter at all.
Actually you don't need WHERE clause at all in this situation. But if you insist then you should write your predicate so it always returns true. This can be done many ways:
Any predicate like:
WHERE 1=1
With column:
WHERE name = name OR name is null
With LIKE:
WHERE name LIKE '%' OR name is null
With passed parameter:
WHERE name = #name OR #name is null
You can think of more of course. But I think you need the last one. Pass NULL from app layer if you want all rows.

Adding NULL to an integer to get the integer result in SQL server

I have say a table, the columns of which will contain integer value but i am not sure if they may contain NULL too. Now lets say I want to add the integers, (along with any NULL) to get the result as an integer even when any column in between is NULL.
I am aware of the ISNULL() function and COALESCE() function. But i would prefer not to use any of these. Nor i want to use CASE. I know that CASE would do what i want, but I am looking for something more better.
Just like we have CONCAT_NULL_YIELDS_NULL to ensure that when a string is concatenated to a null value, the result is not null.
I want something similar to this option. Thanks in advance.
You should probably just use those functions. Assuming you can't:
SELECT SUM(MyIntCol) --filters out NULLs automatically
SELECT CASE WHEN MyIntCol IS NOT NULL THEN MyIntCol ELSE 0 END --default value 0
This isn't really an answer to you but it might be a solution for people running MS SQL Server 2012 or newer.
SELECT IIF(<NullableColumn1> is null, 0, <NullableColumn1>)
+ IIF(<NullableColumn2> is null, 0, <NullableColumn12) ...
FROM <YourTable>
But I assume, that the engine will do the same what is done, when there is CASE used instead.

Why won't this SQL statement work?

I have the following SQL-statement:
SELECT DISTINCT name FROM log WHERE NOT name = '' AND name LIKE '%.EDIT%';
It works fine on Postgres (returns all different names from log, which aren't empty and contain the string '.EDIT'). But on Oracle this statement doesn't work. Any idea why?
SELECT DISTINCT name FROM log WHERE NOT name = '' AND name LIKE '%.EDIT%';
1) Oracle treats '' as NULL, which means the comparison "NOT name = ''" is never true or false; use "IS NOT NULL" instead. But...
2) The second condition "name LIKE '%.EDIT%' will not match an empty string anyway, making the first condition redundant.
So re-write as:
SELECT DISTINCT name FROM log WHERE name LIKE '%.EDIT%';
The empty string in Oracle is equivalent to NULL, causing the comparison to fail.
Change that part of the query to NAME IS NOT NULL
You can rewrite that query without the "NOT NAME=''" clause.
SELECT DISTINCT name
FROM log
WHERE name LIKE '%.EDIT%';
Does that work for you?
If not, in what way does it not work? Does it cause an error? Are the wrong results returned?
Please expand your question with this info :-)