Replace function error trying to export file with odbc - sql

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.

Related

MS Query Excel to Access reserved word, misspelling or punctuation error

I am new to SQL and I am trying to link my excel workbook (2010) to an Access database table (2010) using MS Query to return some summary statistics on our Fleet. I have a parameter for the month which is located in a cell in the excel workbook.
The code I have is below and I get the error message
The SELECT statement includes a reserved word or an argument name that
is misspelled or missing, or the punctuation is incorrect.
I have used the parameter for more simple information and it works perfectly, however, this code seems to be missing something.
SELECT tbl_FLEET_HISTORY.`Type`,
Sum(tbl_FLEET_HISTORY.`Number Railcars`) AS `QTY_Wagons`
Sum(tbl_FLEET_HISTORY.`Number of Platforms: loading units`) AS `QTY_Platforms`
Sum(tbl_FLEET_HISTORY.`Days Leased in Month`) AS `Days_Leased`
Sum(tbl_FLEET_HISTORY.`Days Available`) AS `Days_Available`
Sum(tbl_FLEET_HISTORY.`Daily Rate`) AS `Daily_Rate`
FROM tbl_FLEET_HISTORY tbl_FLEET_HISTORY
WHERE (tbl_FLEET_HISTORY.`Month`=?)
and (tbl_FLEET_HISTORY.`Leased During Month`=`Yes`)
GROUP BY tbl_FLEET_HISTORY.`Type`
I have run through a lot of the answers to this topic but I have not been able to resolve the issue.
I would really appreciate any assistance. If there is any additional information needed please let me know.
In MS Access, the query would look more like:
SELECT fh.Type,
SUM(fh.[Number Railcars]) AS QTY_Wagons,
SUM(fh.[Number of Platforms: loading units]) AS QTY_Platforms,
SUM(fh.[Days Leased in Month]) AS Days_Leased,
SUM(fh.[Days Available]) AS Days_Available,
SUM(fh.[Daily Rate]) AS Daily_Rate
FROM tbl_FLEET_HISTORY as fh
WHERE fh.Month = ? AND
fh.Leased During Month = "Yes"
GROUP BY fh.type;
Notes:
String constants are delimited by double quotes in MS Access.
Backticks are not used at all. Column names are escaped using square braces.
You do not need to escape the aliases you are assigning.
You need commas between the expressions in the SELECT.

Query in VB2010 based on indian language

I am developing a application based on native Indian language. I have to search the database column (in native language) student name in native language. Can anybody help me to solve this issue
my query builder code looks like this and shows syntax error
SELECT [ಕ್ರಮ ಸಂಖ್ಯೆ], [ವಿದ್ಯಾರ್ಥಿಯ ಹೆಸರು] AS Name, [ರಜಿಸ್ಟರ್ ಸಂಖ್ಯೆ], [ಡೈಸ್ ಸಂಖ್ಯೆ], [ವರ್ಗ], [ಕನ್ನಡ], [ಇಂಗ್ಲೀಷ], [ಹಿಂದಿ], [ಗಣಿತ], [ವಿಜ್ಞಾನ], [ಸಮಾಜ ವಿಜ್ಞಾನ], [ದೈಹಿಕ ಶಿಕ್ಷಣ], [ಒಟ್ಟು ಅಂಕಗಳು], [ಶೇಕಡಾ], [ಶ್ರೇಣಿ], [ಒಟ್ಟು ಅಂಕಗಳು ಅಕ್ಷರದಲ್ಲಿ], [ಲ,ಕ/ಕಲಾ ಶಿಕ್ಷಣ], [ಕಂಪ್ಯೂಟರ ಶಿಕ್ಷಣ], [ಫಲಿತಾಂಶ]
FROM Table11
WHERE ವಿದ್ಯಾರ್ಥಿಯ ಹೆಸರುCollate Indic_General_100_CI_AI LIKE N '%ಮ%'
I notice a couple things right off. Because the field in your where clause has a space in it... you have to put delimiters around it. Also, you don't seem to have a space between your field and the Collate keyword... that is necessary. Lastly, you can not have a space between the N and the apostrophe to specify a nvarchar string literal. So try a where clause like this:
WHERE [ವಿದ್ಯಾರ್ಥಿಯ ಹೆಸರು] Collate Indic_General_100_CI_AI LIKE N'%ಮ%'
If you are still getting an error, it could be that your field [ವಿದ್ಯಾರ್ಥಿಯ ಹೆಸರು] is not of a unicode string type. Make sure the field is defined as nvarchar, nchar,...etc.
From your comment it appears that you are querying a MS Access database. The above is for querying a SQL Server Engine. The where clause for access would look like this:
WHERE [ವಿದ್ಯಾರ್ಥಿಯ ಹೆಸರು] LIKE '*ಮ*'

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

Getting a right parenthesis error in 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.

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