I have a table with 9000 items and for test reasons i renamed one object's name into "LOL". Now I want to show me all data records which don't match that string. So, the result has to be 8999 but I only get 620 which is really strange.
Query:
SELECT Count(*) FROM [xxx] WHERE xxx.name = "LOL";
>>Result 1
SELECT Count(*) FROM [xxx] WHERE xxx.name <> "LOL";
>>Result 620
It's a MS Access DB and the datatype of that column is short text.
I really don't get it, why there are so many data records filtered out. It seems like that the NOT EQUAL Operator doesn't work in Access DB. NOT LIKE causes the same result.
Do you have the other columns with null values ?
SELECT Count(*) FROM [xxx] WHERE xxx.name is null or xxx.name <> "LOL";
Related
I am currently working with a MS SQL database on Windows 2012 Server
I need to query only 1 column from a table that I only have access to read, not make any kind of changes.
Problem is that the name of the column is "Value"
My code is this:
SELECT 'Value' FROM table
If I add
`ORDER BY 'Value'`
The issue is that the query is returning an empty list of results.
Things I've tried already
I tried replacing ' with `"' but this didn't work either.
I also tried writing SELECT * instead of SELECT VALUE
Using the table name in the SELECT or ORDER clauses again didn't help
You are claiming that this query:
SELECT 'Value'
FROM table
ORDER BY 'Value'
Is returning no rows. That's not quite correct. It is returning an error because SQL Server does not allow constant expressions as keys for ORDER BY (or GROUP BY for that matter).
Do not use single quotes. In this case:
SELECT 'Value' as val
FROM table
ORDER BY val;
Or, if value is a column in the table:
SELECT t.Value
FROM table t
ORDER BY t.Value;
Value is not a reserved word in SQL Server, but if it were, you could escape it:
SELECT t.[Value]
FROM table t
ORDER BY t.[Value];
it looks like your table has null values. and because of the order by all null values come first.
try to add filter like this
select Value FROM table
where Value is not null and Value <> ''
order by Value
I have to compare comma separated values with a column in the table and find out which values are not in database. [kind of master data validation]. Please have a look at the sample data below:
table data in database:
id name
1 abc
2 def
3 ghi
SQL part :
Here i am getting comma separated list like ('abc','def','ghi','xyz').
now xyz is invalid value, so i want to take that value and return it as output saying "invalid value".
It is possible if i split those value, take it in temp table, loop through each value and compare one by one.
but is there any other optimal way to do this ??
I'm sure if I got the question right, however, I would personally be trying to get to something like this:
SELECT
D.id,
CASE
WHEN B.Name IS NULL THEN D.name
ELSE "invalid value"
END
FROM
data AS D
INNER JOIN badNames B ON b.Name = d.Name
--as SQL is case insensitive, equal sign should work
There is one table with bad names or invalid values if You prefer. This can a temporary table as well - depending on usage (a black-listed words should be a table, ad hoc invalid values provided by a service should be temp table, etc.).
NOTE: The select above can be nested in a view, so the data remain as they were, yet you gain the correctness information. Otherwise I would create a cursor inside a function that would go through the select like the one above and alter the original data, if that is the goal...
It sounds like you just need a NOT EXISTS / LEFT JOIN, as in:
SELECT tmp.InvalidValue
FROM dbo.HopeThisIsNotAWhileBasedSplit(#CSVlist) tmp
WHERE NOT EXISTS (
SELECT *
FROM dbo.Table tbl
WHERE tbl.Field = tmp.InvalidValue
);
Of course, depending on the size of the CSV list coming in, the number of rows in the table you are checking, and the style of splitter you are using, it might be better to dump the CSV to a temp table first (as you mentioned doing in the question).
Try following query:
SELECT SplitedValues.name,
CASE WHEN YourTable.Id IS NULL THEN 'invalid value' ELSE NULL END AS Result
FROM SplitedValues
LEFT JOIN yourTable ON SplitedValues.name = YourTable.name
I'm working with a huge Access (.mdb) database, for my bad luck I can't change any table design, just able to create queries, well...
Having this table (part of):
Table1
Id Autonumber
Name Text(50) Not Null
Prod Text(8) Null
where Prod field is a date in format "ddMMyyyy", and not required.
With values like this:
Id Name Prod
------------------------
1 KX_W 06061988
2 AXR (null)
3 ELR 03021957
Then I'm trying this query, to get records according to a date:
PARAMETERS [#basedate] Date;
SELECT
Table1.Id,
Table1.Name
FROM
Table1
WHERE
((Table1.Prod) Is Not Null) AND
(GetDate(Table1.Prod) >= [#basedate])
ORDER BY
Table1.Id;
*GetDate() is a VBA module function that returns a Date value (dd/MM/yyyy) from a string argument.
When executing the query, I'm getting this message:
"This expression is typed incorrectly or it is too complex to be evaluated..."
The table is about 50K-record sized, but I'm pretty sure that's not the reason, there are other queries around this database, 10x more complex and run very quick!
I've tried with a piece of this table having no null values on Prod field and works fine, but when I try with the entire table, the message pop up; what is that I'm missing?
As a last resort, you can try to use a nested subquery.
SELECT SUB.ID, SUB.Name
FROM
(SELECT t.ID, t.Name, t.Prod WHERE t.Prod Is Not Null) AS SUB
WHERE GetDate(SUB.Prod) >= [#basedate]
ORDER BY SUB.ID
I have an SQL query that I am looking to optimize.
SELECT *
FROM QUEUE_SMS_ALERT Q1
where ALERT_ORIGIN = "FOO"
AND RECORD_ID is null
and PHONE NOT IN (
SELECT DISTINCT PHONE
FROM QUEUE_SMS_ALERT Q2
where Q2.ALERT_ORIGIN = "BAR"
);
Basically need to get all rows where ALERT_ORIGIN is "FOO" Which do not have a corresponding row in the same table with ALERT_ORIGIN "BAR". The table contains abt 17000 rows and there are only abt 1000 records with ALERT_ORIGIN "BAR". So my query is supposed to give me abt 16000 rows.
EDIT : The current query is very slow. I do not have any indexes currently.
I'm guessing that you have NULL values in the phone column which means NOT IN doesn't work (so it's "fix" not "optimise"). So I've written it with NOT EXISTS:
SELECT *
FROM QUEUE_SMS_ALERT Q1
WHERE
Q1.ALERT_ORIGIN = 'FOO'
AND
Q1.RECORD_ID is null
AND
NOT EXISTS (SELECT *
FROM QUEUE_SMS_ALERT Q2
WHERE
Q2.ALERT_ORIGIN = 'BAR'
AND
Q1.PHONE = Q2.PHONE)
If it is slow rather than "wrong" then you need to use indexes. What do you have now?
For this query, you need an index on (ALERT_ORIGIN, PHONE, RECORD_ID).
Note: use single quotes for string delimiters
I have 2 similar queries
select *
from openquery(powerschool,
'select *
from TEACHERS
where teachernumber is not null
and schoolid=''1050''
and teacherloginid is not null
order by teachernumber')
and
SELECT *
from openquery(powerschool,
'SELECT NVL(teachernumber,'''')
from TEACHERS
where teachernumber is not null
and schoolid=''1050''
and teacherloginid is not null
order by teachernumber')
The first one is giving me 182 rows while the second one gives me 83.
What's wrong with the queries?
Second query never would return a null for the teachers table because of the NVL() so it could return more records depending on the data.
basically the " and teacherloginid is not null " never gets hit because you replace the nulls with ""
Just thoughts...
Same server? That is, linked server is different in target or credentialss o you are reading a different "TEACHERS" table
What does running both linked SQL statement actually on the linked server (not locally) give you?