Getting a right parenthesis error in SQL - sql

I am getting an error that says I am missing right parenthesis, but I'm not. I dont understand what is going wrong with my query.
CREATE VIEW HB_INV(Book Title, Publisher Name, Price, Units On Hand) AS
SELECT BOOK.TITLE, PUBLISHER.PUBLISHER_CODE||'-'||PUBLISHER.PUBLISHER_NAME,
BOOK.PRICE, INVENTORY.ON_HAND
FROM BOOK, PUBLISHER, INVENTORY
WHERE BOOK.PUBLISHER_CODE = PUBLISHER.PUBLISHER_CODE
AND BOOK.BOOK_CODE = INVENTORY.BOOK_CODE;

If this is SQL Server you need Square Brackets around [Units On Hand] and also, if that is a concatenation for publisher code, use '+' instead of '||'

The problem is Units on Hand. Make it all one word.
Edit starts here
Actually, it's also "Book Title" and "Publisher Name". Make all the fields in your view single words.

Related

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?

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.

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.

What is wrong with my SQL in SelectLayerByAttribute?

My code is arcpy.SelectLayerByAttribute_management("polygons_file", "NEW_SELECTION", "Shape_Area=(SELECT MAX(Shape_Area) FROM polygons_file")
I am getting an error "The SQL expression is invalid."
How can I fix it?
Subqueries should work, but you have a mismatched parentheses.
Wrong: Shape_Area=(SELECT MAX(Shape_Area) FROM polygons_file
Right: Shape_Area=(SELECT MAX(Shape_Area) FROM polygons_file)
Also note the different field delimiters to use depending on your data source (e.g. shapefile or geodatabase); you may need to include quotation marks around Shape_Area, e.g.
arcpy.SelectLayerByAttribute_management('polygons_file', 'NEW_SELECTION', '"Shape_Area" = (SELECT MAX("Shape_Area") FROM polygons_file)')

Replace function error trying to export file with odbc

select id, "last name", "first name", address, "zip code", "Home phone", "Mobile Phone",
REPLACE([email address],',','') as email, "date of birth",
iif ( len([recall date]) = 3,
left("recall date",1)+'/01/20'+ right("recall date",2),
left("recall date",2)+'/01/20'+ right("recall date",2))
as Recalldate
from "patient information"
Im trying to replace a comma that is at the end of each email address somehow its coming from access.
The current query is causing an error
9/18/2014 7:11:01 PM error System.Data.Odbc.OdbcException : ERROR [42000] [Microsoft][ODBC Microsoft Access Driver] Extra ) in query expression '[left([Email address])'.
at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader)
at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Odbc.OdbcCommand.ExecuteReader()
Depending on the provider that you are using for querying the table(s), the functions Replace, IIF, Len, Left and Right might not be available or have different meaning. Without any details on how this query is executed, from where and against with what type of database that you are doing this, it's impossible to tell you what the error might be and how to correct it.
One possibility would be to replace the single quote with double quotes as in:
Replace([email address],",","")
but this is only a guess. You don't seem to have any problem later in your query expression with single quotes; so I don't think that this is the problem in your case. I showing this just to give you an example of what kind of stuff can go wrong when using different providers or different databases.
I've also noticed that sometimes, you use the [...] to delimit your field names and other times, you use the double quotes "...". Not a good ideal to mix this kind of stuff as some providers will accept one notation but not the others.
Finally, by experience, I can tell you that using any space in a field or a table name such as "patient information" is a very, very bad idea. Stop doing that or later, you might deeply regret it when you will query your database from a system that doesn't or badly support that type of stuff.

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).