SQL. How should i write NOT LIKE statement - sql

SELECT *
FROM [Table]
WHERE (Izdava NOT LIKE NULL)
// how to check if Izdava is not NULL

This should work:
SELECT * FROM [Table] WHERE Izdava IS NOT NULL

SELECT * FROM [Table] WHERE (Izdava IS NOT NULL)

LIKE NULL is meaningless. It doesn't make sense. LIKE is used for comparing a partial string using wildcards, or a complete string without wildcards.
Depending on the RDBMS you want NOT IS NULL,
SELECT * FROM [Table] WHERE (NOT Izdava IS NULL)

I think this is what you want.
SELECT *
FROM [Table]
WHERE Izdara Is Not NULL
Information about NULL values from MSDN:
Following is information about nulls:
To test for null values in a query, use IS NULL or IS NOT NULL in the
WHERE clause.

If you are checking for not null, dont use the like clause. Just write
select * from tablename where columnmame is not null

If it's MSSQL or Oracle then do this:
SELECT *
FROM [Table]
WHERE Izdava IS NOT NULL

Related

sql repeat regex pattern unlimited times

I need to select where column contains numbers only and ends with a hyphen
I'm running SQL Server Management Studio v17.9.1
I have tried:
select * from [table] where [column] like '[0-9]*-'
select * from [table] where [column] like '[0-9]{1,}-'
select * from [table] where [column] like '[0-9]{1,2}-'
none of these work. The expression ([0-9]*-) works in any regex tester I've run it against, SQL just doesn't like it, nor the other variations I've found searching.
You can filter where any but the last character are not numbers and the last is a dash. DATALENGTH/2 assumes NVARCHAR type. If you're using VARCHAR, just use DATALENGTH
SELECT
*
FROM
[table]
WHERE
[column] like '%-'
AND
LEFT([column], (datalength([column])/2)-1) NOT LIKE '%[^0-9]%'
SQL Server does not support regular expressions -- just very limited extensions to like functionality.
One method is:
where column like '%-' and
column not like '%[^0-9]%-'
You can use left() and right() functions as below :
with [table]([column]) as
(
select '1234-' union all
select '{123]' union all
select '1234' union all
select '/1234-' union all
select 'test-' union all
select '1test-' union all
select '700-'
)
select *
from [table]
where left([column],len([column])-1) not like '%[^0-9]%'
and right([column],1)='-';
column
------
1234-
700-
Demo

How to ask where with calling function? - SQL Server

I want to ask a Function in the where clause.
The code:
SELECT * FROM MyTable
WHERE Field1=10
and dbo.MyFunction(Field2,Field3,Field4) = NULL
And this returns me an empty table even though there are correct values in the table, why is this happening? What could be the reason?
SELECT * FROM MyTable
WHERE Field1=10
and dbo.MyFunction(Field2,Field3,Field4) **is** NULL
and not = NULL
You need to define like dbo.MyFunction(Field2,Field3,Field4) is NULL instead of dbo.MyFunction(Field2,Field3,Field4) = NULL
SELECT * FROM MyTable
WHERE Field1=10
and dbo.MyFunction(Field2,Field3,Field4) is NULL
In SQL you have to use is null or is not null instead = null.
I suggest you do not use function in your where clause it is not a good practice. You can use cte as below:
with
cte
as
(
select
* ,
dbo.MyFunction(Field2,Field3,Field4) as [your_function_field]
from
MyTable
where
Field1=10
)
where
[your_function_field] is null

SQL Select empty

I have the following code in Oracle 11:
select xmlelement("foo", xmlagg(xmlelement("bar",myValues))) from someTable where rownum = 0; --Changing the rownum from 0 should give me values
The output currently is: <foo></foo>
I would instead like this to return nothing, or null, when no rows are select. Otherwise it'll be a XMLtype with the aggregated data like how I have it above.
How would I be able to achieve this for the case when no rows are selected?
You can use case select:
select case when count (*) != 0 then xmlelement("foo", xmlagg(xmlelement("bar",myValues))) end
You can convert to CLOB using getClobval function and do a comparison.
SELECT
xml
FROM
(
SELECT
xmlelement("foo",xmlagg(xmlelement("bar",myvalues) ) ).getclobval() xml
FROM
sometable
)
WHERE
TO_CHAR(xml) != '<foo></foo>';

NVL Column and NULL

I have rows as shown below
ProductId ProductName ProductDesc ProductLoc
101 Camel Pencil B-10
102 Parker Pen
103 Mirado Pen C-10
When I execute the following SQL query
SELECT *
FROM tablename
WHERE productloc = NVL ('', productloc)
It gives me the 1st and 3rd row, what I would like to achieve is if productloc is null in where condition of the SQL, then I should get all three rows.
How can I get the desired output.
select * from tablename;
Is what you need in that case because your where gives no effect. You compare column with itself.
If you want to filter and include nulls you can do (but probably replace one productloc with some value:
select * from tablename where productloc = productloc or productloc is null;
Or:
select * from tablename where nvl(productloc, 'SOME_UNIQUE_VAL') = nvl(productloc, 'SOME_UNIQUE_VAL');
and also replace one of productloc by some value.
Try something like :
SELECT *
FROM tablename
WHERE nvl(productloc,'zzz') = (case when productloc is null then 'zzz' else productloc end )
Here zzz is some dummy value, which otherwise should not be present as value in particular column.
I suppose three different solutions:
If you want all rows (with productloc null and valued) you can write the following query:
SELECT *
FROM tablename
Without WHERE clause.
If you want extract all rows with productloc has a specified value or is null, so you can write the following query:
SELECT *
FROM tablename
WHERE productloc IS NULL OR productloc = YOURVARIABLE
or (the last, without use OR condition)
SELECT *
FROM tablename
WHERE NVL(productloc, YOURVARIABLE) = YOURVARIABLE
I assume you are using Oracle. In Oracle "NULL" is not a value and because of it any compare function will return false.
I recomend:
SELECT *
FROM tablename
WHERE (productloc = productloc OR productloc IS NULL)
Hope it helps,
Sérgio

SQL: Select distinct - without highcase/lowcase duplicate

My query:
SELECT distinct [ID], [IDGROUP], [DESCRIPTION]
FROM table
My result problem:
1, 1, Hello
1, 1, hello
How can I set a filter where I do not select duplicates where the difference is only the high/low case letter??
Try running this query.
SELECT distinct [ID], [IDGROUP], [DESCRIPTION] COLLATE SQL_Latin1_General_CP1_CI_AS FROM table
Here you are basically saying ignore case on the DESCRIPTION column
CI = Case insensitive
AS = Accent sensitive.
I guess mysql engine does not duplicate based on letter case by default.
That is both Hello and hello is considered same.
Not sure if this is the best way but this worked for me:
SELECT distinct [ID], [IDGROUP], Upper([DESCRIPTION]) as [DESCRIPTION]
FROM table