Cannot figure out SQL Select statement in Access 2016 - sql

I'm having some trouble understanding WHY a select statement isn't working in a query I'm making.
I've got the SELECT and FROM lines functioning. With just those, ALL results from my selected table are displayed - 517 or so
What I want to do is display results based on a pattern using LIKE - What I have so far
SELECT *
FROM Tbl_ServiceRequestMatrix
WHERE Tbl_ServiceRequestMatrix.[Application/Form] LIKE 'P%';
This returns 0 results - despite the fact that the column selected DOES have entries that start with 'P'
I also tried utilising brackets, see if that was the issue - still displays 0 results:
SELECT *
FROM Tbl_ServiceRequestMatrix
WHERE ((Tbl_ServiceRequestMatrix.[Application/Form])='p%');
Can any one help me understand why my WHERE ** LIKE statement is causing 0 results to be displayed?

The wildcard character in MS Access is (by default) * instead of %:
WHERE Tbl_ServiceRequestMatrix.[Application/Form] LIKE "P*"

LIKE Statement has different parameters in different sql languages.
In MS Access you need * Instead of % in LIKE Statement.

Related

SQL full text search behavior on numeric values

I have a table with about 200 million records. One of the columns is defined as varchar(100) and it's included in a full text index. Most of the values are numeric. Only few are not numeric.
The problem is that it's not working well. For example if a row contains the value '123456789' and i look for '567', it's not returning this row. It will only return rows where the value is exactly '567'.
What am I doing wrong?
sql server 2012.
Thanks.
Full text search doesn't support leading wildcards
In my setup, these return the same
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'28400')
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"2840*"')
This gives zero rows
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"*840*"')
You'll have to use LIKE or some fancy trigram approach
The problem is probably that you are using a wrong tool since Full-text queries perform linguistic searches and it seems like you want to use simple "like" condition.
If you want to get a solution to your needs then you can post DDL+DML+'desired result'
You can do this:
....your_query.... LIKE '567%' ;
This will return all the rows that have a number 567 in the beginning, end or in between somewhere.
99% You're missing % after and before the string you search in the LIKE clause.
es:
SELECT * FROM t WHERE att LIKE '66'
is the same as as using WHERE att = '66'
if you write:
SELECT * FROM t WHERE att LIKE '%66%'
will return you all the lines containing 2 'sixes' one after other

Access SQL: like function on a number field

I have ,for example, this table in a Microsoft Access database:
id numeric
context text
numberfield numeric
I want to select every record that ends with 9 in the column"numberfield". This gives a problem because it is a numeric field and as a result I can not use the following SQL:
select * from table where numberfield like "%9"
A solution is that I change the numberfield to a text. But this gives a problem because there are several users and the change might give a problem in the future. Is there an option to select on the ending when it is a number field?
That sound a little fishy.. are you sure you can use that query? Don't know about Access but almost any other DBMS allows it.
If it really doesn't work, you can do this:
select * from table where STR(numberfield) like "*9"
EDIT: Maybe it didn't work because you used % which is used with * in Access :
select * from table where numberfield like "*9"
Numbers are numbers, so use Mod for this:
select * from table where numberfield mod 10 = 9
Instead of casting to string and comparing, just extract the rightmost digit with a MOD operation.
Edit your query as follows:
SELECT *
FROM table
WHERE ((([numberfield] Mod 10)=9));

Why is my UPDATE query returning fewer results than the equivilent SELECT query?

I'm trying to run an UPDATE query in Access 2010 to remove trailing spaces from a field. Before running the full query, I'm writing a test query limited by an ID that returns 1 result, in case anything goes horribly wrong.
The SELECT version of the query returns one result, as expected:
SELECT dbo_Contact.ContactID, dbo_Contact.Pref
FROM dbo_Contact
WHERE (((dbo_Contact.ContactID)=11906) AND ((dbo_Contact.Pref) Like "% "));
However, when I change it to an UPDATE query, it says "You are about to update 0 row(s)."
The UPDATE query is below:
UPDATE dbo_Contact SET dbo_Contact.Pref = Left([Pref],(Len([Pref])-1))
WHERE (((dbo_Contact.ContactID)=11906) AND ((dbo_Contact.Pref) Like "% "));
What am I doing wrong? Being that the WHERE filter is the same, I'm assuming it's in the expression I'm using as the value to update to. If this is the case, what's wrong with that?
For the most part, the Access wildcard is *, not %. There are exceptions, but I am not sure if you have such a setup.

Access And/Or exclusions

I have some sample data like:
|H.RISK|NOTE|NORMAL|
The | are actually in the data, it's a String.
I am using Access, and am trying to exclude records that contain RISK in the String.
Sample query:
SELECT * FROM someTable WHERE (UCase(someTable.Field) NOT LIKE '*RISK*') AND (UCase(someTable.Field) NOT LIKE '*Blah*') AND someTable.SomeOtherField <> 4;
The problem is, the query is returning the above sample record, even though it does contain the string RISK.
I've tried the same query but switched to OR instead of AND but get the same results.
How can I properly structure this query to exclude records which contain certain strings?
Seeing that it appears you are running using the SQL syntax, try with the correct wild card.
SELECT * FROM someTable WHERE (someTable.Field NOT LIKE '%RISK%') AND (someTable.Field NOT LIKE '%Blah%') AND someTable.SomeOtherField <> 4;

CHARINDEX with SQL returns partial matches

I'm having trouble with the following SQL query;
SELECT *
FROM Table
WHERE ID='510' AND CHARINDEX(requires,'Absolute Position Anchor') > 0
I've been using this code for a while but today it all of a sudden starts returning rows where the requires column only contains 'Position Anchor'. Has my query been wrong all along or might something have changed in my MS SQL server settings?
You've got the syntax flipped:
CHARINDEX('string to find','string to look in')
Change to:
SELECT *
FROM Table
WHERE ID='510' AND CHARINDEX('Absolute Position Anchor',requires) > 0
Or use LIKE, as Gordon Linoff suggests:
SELECT *
FROM Table
WHERE ID='510' AND requires LIKE '%Absolute Position Anchor%'