Why am I getting a `Data type mismatch` error when I add "CF" to the end of my search string in a SQL statement in Access? - sql

The following query (qryCurLotNewProducts) produces a data set that I want process further with another query (qryBNP_CFRecordset):
//qryCurLotNewProducts
SELECT tblNewProducts.*
FROM tblNewProducts INNER JOIN tblCurLot ON (tblCurLot.CatalogNum = tblNewProducts.CatalogNum) AND
(tblNewProducts.LotNum = tblCurLot.CurLot);
When I run this second query to list only the "CF" products found in the first query, I get the `Data type mismatch in criteria expression' error.
//qryBNP_CFRecordset
SELECT qryCurLotNewProducts.*, tblABCategory.UNSPSC, tblAmount.ProductSize
FROM tblAmount RIGHT JOIN (tblABCategory RIGHT JOIN qryCurLotNewProducts ON tblABCategory.ABCategory = qryCurLotNewProducts.ABCategory) ON tblAmount.Amount = qryCurLotNewProducts.Amount
WHERE (((qryCurLotNewProducts.CatalogNum) Like "A700-###CF") AND ((qryCurLotNewProducts.DateEntered) Between #1/1/2000# And #3/1/2020#))
ORDER BY qryCurLotNewProducts.CatalogNum, Abs(qryCurLotNewProducts.LotNum);
If I remove the CF from the search string (so "A700-###"), the query correctly outputs a list containing all items that contain that pattern:
If I use strings like "A700-####F" or "A700-###ZZ" or other combinations like that, I don't get an error but rather an empty results set.
Notably, "A700-001CF", "A700-002CF", etc all create the data type error. It seems there is something about the CF key combination that is causing trouble.
Has anybody else ever seen this issue? Do I need to use some kind of delimiter to tell SQL to not view CF as some kind of special switch?

Abs(qryCurLotNewProducts.LotNum) wont work with the values for Products ending in CF. Your LotNum-Column has a text-type.
Edit: Your LotNum-Column has a text-type as you can see in your first screenshot.

Related

Querying full and sub-strings via multi-valued parameter using SQL

I am building a report with Microsoft SSRS (2012) having a multi-value parameter #parCode for the user to filter for certain codes. This works perfectly fine. Generally, my query looks like this:
SELECT ...
FROM ...
WHERE
TblCode.Code IN (#Code)
ORDER BY...
The codes are of following type (just an excerpt):
C73.0
C73.1
...
C79.0
C79.1
C79.2
Now, in additon to filtering for multiple of these codes I would like to als be able to filter for sub-strings of the codes. Meaning, when the user enters (Example 1)
C79
for #parCodes The output should be
C79.0
C79.1
C79.2
So eventually the user should be able to enter (Example 2)
C73.0
C79
for #parCodes and the output would be
C73.0
C79.0
C79.1
C79.2
I managed to implement both functionalities seperately, so either filtering for multiple "complete" codes or filterting for sub-string of code, but not both simultaneously.
I tried to do something like
...
WHERE
TblCode.Code IN (#parCode +'%')
ORDER BY...
but this screws up the Example 2. On the other hand, if I try to work with LIKE or = instead of IN statement, then I won't be able to make the parameter multi-valued.
Does anyone have an idea how to realize such functionality or whether IN statement pared with multi-valued parameters simply doesn't allow for it?
Thank you very much!
Assuming you are using SQL server
WHERE (
TblCode.Code IN (#parCode)
OR
CASE
WHEN CHARINDEX('.', Code)>0 THEN LEFT(TblCode.Code, CHARINDEX('.', TblCode.Code)-1)
ELSE TblCode.Code
END IN (#parCode)
)
The first clause makes exact match so for your example matches C73.0
The second clause matches characters before the dot character so it would get values C79.0, C79.1, C79.2 etc
Warning: Filtering using expressions would invalidate the use of an index on TblCode.Code

Access 2007 CInt Query Issue

There is probably a pretty basic answer to this question, but I'm pulling my hair out trying to resolve my issue. I'm using Access 2007.
My query is shown below:
SELECT Pricing.*
FROM OrderReceipt_be
INNER JOIN Pricing ON CInt(OrderReceipt_be.[Pricing Table Option Code]) = Pricing.ID
WHERE OrderReceipt_be.[PO_Number] = PONumber();
For whatever reason, the [PO_Number] field is stored as text against my key which is a long int. This is why I'm trying to convert it to an integer.
However, when I run my query I get the error
"Compile error. in query expression CInt(OrderReceipt_be.[Pricing
Table Option Code]) = Pricing.ID".
I've done some basic research and it seems like the most common issue is that I'm missing a reference library. Howver, having gone through the entire list, I don't see any references that are tagged as "Missing" so it must be something else. I've also tried disabling and re-enabling all enabled reference libraries to see if that helps, but so far nothing.
Any thoughts?
If you can have codes of Null, try:
SELECT Pricing.*
FROM OrderReceipt_be
INNER JOIN Pricing ON Val(Nz(OrderReceipt_be.[Pricing Table Option Code])) = Pricing.ID
WHERE OrderReceipt_be.[PO_Number] = PONumber();
or try the reverse conversion:
SELECT Pricing.*
FROM OrderReceipt_be
INNER JOIN Pricing ON OrderReceipt_be.[Pricing Table Option Code] = CStr(Pricing.ID)
WHERE OrderReceipt_be.[PO_Number] = PONumber();

Syntax error in an Open-SQL statement

What's wrong with this statement?
SELECT aufk~aufnr
zmm_limit_co~vd zmm_limit_co~matkl_code
zmm_limit_matkl~sign
FROM aufk
JOIN zmm_limit_co ON zmm_limit_co~auart = aufk~auart
left JOIN zmm_limit_matkl
on zmm_limit_matkl~matkl = zmm_limit_matkl~matkl_code
INTO CORRESPONDING FIELDS OF table lt_input
WHERE aufk~aufnr = <lf_new_pos>-aufnr.
When I'm trying to execute program, ABAP gives me an error:
'The elements in the "SELECT LIST" list must be separated using commas.'
I suppose, that the error is somehow connected with JOINs, when I'm removing "left" from it - it's compiling just fine, but with it.
You are (probably inadvertently) mixing the old (now obsolete) and new syntax of the OpenSQL SELECT statement, triggering this rather less-than-helpful error message. Check the release-specific change notes for some details on the changes. However, in the example given, I believe that the second join condition is the problem: You're not joining the contents of zmm_limit_matkl with any of the other two tables, but with itself. That doesn't look right and might confuse the compiler.

SQL Full Text Contains not returning expected rows

I have a Body column that is full text indexed and is nvarchar(max)
One row has this in the Body column
You want slighty mad this sat the 60th runing of the 3peaks race! Peny-ghent whernside and inglbauher! Only in yorkshire!
If I run: select body from messages where CONTAINS(Body,'you') it doesn't return any data.
If I run the below adding wildcards select messageid,body from messages where CONTAINS(body,'"*you*"') it still doesnt return the data.
Can you help me understand what's going on please?
Thanks
UPDATE : It makes no difference if its you or You, either way no results
It can be case sensitivity issue. Try with select messageid,body from messages where CONTAINS(body,'"*You*"') and see if you are getting the result or not
A full text catalog has a set of words in a “stoplist” that it won’t search on as SQL Server considers them “unimportant for search purposes”
To get this you can run
select ssw.*
from sys.fulltext_system_stopwords ssw
where ssw.language_id = 1033;
Below are the words it won’t search on and you’ll see it contains “you” hence why it didn’t find my data.

Where Clause with Alias Visual Basic & SQL

I have inherited a MS database, to work from, this database also links to other programs so I don't want to change the database tables itself.
I'm using Visual Basic 2010,
What I need to do is have a range of filters on this table and then one extra filter entered by the user.
e.g. they enter '50' and range '5' I need to search the dataset using the range of '45 to 55'
This is my code so far for the dataset:
SELECT [CUTTER NO]
,CUTTER_ID
,[SIZE-Inches]
,[MM-Across]
,[MM-Round]
,TYPE
,[LEADING EDGE]
,[CUTTER TYPE]
,ACROSS
,ROUND
,[WIDTH PAPERmm]
,[GAPS ACROSSmm]
,[GAPS ROUNDmm]
,[Serial Number]
,[T G]
,Repeat
,[Repeat MM]
,[L&G]
,Notes
FROM [Cutter List]
WHERE (TYPE <> 'DISCONTINUED')
AND (TYPE <> 'SPEC')
AND (CUTTER_ID <> NULL)
AND ([CUTTER TYPE] = 'MP')
AND (TYPE <> 'BUTT')
ORDER BY CUTTER_ID, [MM-Across]
What I need to type into this SQL is:
WHERE [MM-Across] LIKE #[MM-Across] and [MM-Round] LIKE #[MM-Round]
Which from what I can tell on the net is wrong as I cannot have [] in a where.
I even tried :
SELECT [MM-Across] AS mmacross
FROM [Cutter List]
WHERE ('mmacross' LIKE '#mmacross')
This it accepts but I get an different error appear saying
"The Schema returned by the new query differs from the base query."
What am I doing wrong? I don't understand the last error or how to avoid this.
Two things:
You can definitely have brackets "[..]" in WHERE clauses, they just have to be in the correct places (just like anywhere else), and
You cannot use the brackets in variable or parameter references, and that means that, unlike columns, you cannot have special characters in their names, like "-" in "#MM-Across", so just change your parameter names to something like "#MM_Across".
Note that the column names are fine, it's your parameter and/or variable names that you have to change (I cannot tell which these are from your snippet).
So instead of this:
WHERE [MM-Across] LIKE #[MM-Across] and [MM-Round] LIKE #[MM-Round]
Try this:
WHERE [MM-Across] LIKE #MM_Across and [MM-Round] LIKE #MM_Round
Of course, you will also have to change the parameter/variable names wherever they are declared and passed in. If you post the code that does this, I can show you how to change that also (thoguh it may be obvious by now).