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 :-)
Related
I'm attempting to write a query in Pervasive SQL which matches on a "LIKE" clause, but without case sensitivity.
As an example, I want the following query to match both "john", "John", and "JOHN". Currently, this is case sensitive.
SELECT name FROM table WHERE name LIKE ?
In T-SQL, I would just wrap UPPER around both parts of the WHERE clause, like this:
SELECT name FROM table WHERE UPPER(name) LIKE UPPER(?)
However, placing any functions to the right of the WHERE clause fails with a syntax error.
How can I achieve a case-insensitive search?
The only way I can think of is to change the case of the value that's coming in before you create the SQL Statement. Something like this C#ish code:
string value = "world";
sql = "SELECT name FROM table WHERE name LIKE = '" + value.ToUpper();
Or, even better use Parameters and set your value before you set the parameter.
You're right, having a function on the right side the LIKE will cause a Syntax Error. You might want to report it as a bug to Actian.
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.
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.
I have the following query in a stored procedure in SQL server:
SELECT TLI.LESNumber
,COUNT(TLT.PL)
INTO #PWCM
FROM #tmpLESImport TLI
INNER JOIN tbl_LES L
on TLI.LESNumber=L.NUMB
WHERE ISNULL(L.DELT_FLAG,0)=0
AND L.SCHL_PK=#SCHL_PK
AND TLI.PL IS NOT NULL
AND LEN(TLI.PL)>0
GROUP BY LESNumber
HAVING COUNT(PL)>1
When the query is run I get the following error:
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
Can anyone tell me why? #PWCM does not appear anywhere until this query.
When you SELECT INTO a table, it creates the table (in this case, a temp table). In order to create a table, each column needs a name, which your count column does not. You just need to give it a name:
SELECT TLI.LESNumber,COUNT(TLT.PL) [NumRecords]
INTO #PWCM
FROM #tmpLESImport TLI
...
I had this error for this query
SELECT
CASE WHEN COALESCE([dbo].[my-table].[field],"") = '...' THEN 'A'
WHEN COALESCE([dbo].[my-table].[field],"") = '...' THEN 'B'
...
END AS aaa INTO ##TEMPTABLE
FROM [dbo].[my-table]
Turns out I had to change the "" inside the COALSCE into ''.
Solved it for me
I just came across this, and the core reason was actually related to an errant set of brackets in the code which made the engine think there was a missing alias. Something along the lines of:
select *
from SOME_TABLE
where x = 1
[]
A stringified version of the query included a parameter list for logging, but that was being issued as the query instead of the actual query object. Deleting [] at the end resolved it.
I have a table and one of the columns holds web addresses like: 'http://...' or 'https://...'.
The problem is that there are some invalid entries, like 'shttp://...' or '#http//...' (the first character is invalid) and I want to correct all of them.
I use the following SQL statement:
'SELECT [...] FROM MyTable WHERE WebAddress LIKE '_http%'
and I successfuly get the problematic rows.
But how am I going to change/correct all of them using an UPDATE statement?
If you have some other solution please share it!
Simply change the SELECT to an UPDATE (of course, with some syntax changes) with a "fix" expression
UPDATE
MyTable
SET
WebAddress = SUBSTRING(WebAddress, 2, 8000)
WHERE
WebAddress LIKE '_http%'
You Can use Sub string property as you can trim odd letters .Also like '_word start' suitable for your question