Incorrect syntax near '-' - sql

i am trying to run a query in another server and need to specify it in the select statement but the server name has got an '-' in it, like server-name.
this is producing the error in the title.
How can i fix it?

Use square brackets [] around the servername:
select *
from [server-name].[db-name].[schema-name].[table-name]

I came across an SQL query on some website, which was supposed to be correct, but I was getting the same error. It appears, that some websites format text and swap the minus sign with some Office-like abomination.
Incorrect minus character: –
Correct one: -

I also came across this error when I work with spring boot and MSSQL db. We may get this error, when we did not define the schema and the table name correctly.
I have included the below schema name with square brackets in the model class and I could overcome this issue.
#Table(name = "FIELDS_PARAM",schema = "[sample-schema]")

Related

SQLSyntaxErrorException Using LTRIM to trim character 'x' in query

I using TRIM function to trim some characters in query, I using hibernate following is my query.
from ABean s where s.cId in (select ca.id from CBean ca where LTRIM(ca.refNumber,'0') = LTRIM('$ref$','0') and ca.valid = 0)
$ref$ is replace with actual value in query.
I am seeing a different behaviour when I am running with DB2 and When I am running with Mockito test (Using In memory DB).
With DB2 this query is working fine but with Mockito in memory db I am getting java.sql.SQLSyntaxErrorException, Error is something like this.
Syntax error: Encountered "," at line 1, column {column_number_in_actual_query}.
I am not able to make it working with in memory db, Is there anything wrong I am doing?
Thanks.
in IBM DB2, in the SYSIMB schema, LTRIM takes a second argument of characters being trimmed like you have (see here). However, in the SYSFUN schema (and in most other SQL implementations) it only takes one argument and assumes you are trimming whitespace (see here).
Based on the error it looks like the interpreter wasn't expecting a comma, so it's probably trying to use the more standard version of the function and failing when it sees the second argument.
based on the documentation for function references you should be able to replace LTRIM with SYSIBM.LTRIM

How would I fix these "ORA-00933: SQL command not properly ended" "ORA-00923: FROM keyword not found where expected" errors?

This Statement:
SELECT id, units, cost FROM inventory_list WHERE cost <= 20;
Gives me:
ORA-00923: FROM keyword not found where expected
While this statement:
SELECT * FROM items WHERE ilt_id = 'il010230126' OR ilt_id = 'il010230128';
Gives me:
ORA-00933: SQL command not properly ended
Not sure about this and may be version dependent (below link is for oracle 10g... but you can see on this site
https://docs.oracle.com/cd/B19306_01/em.102/b40103/app_oracle_reserved_words.htm
That cost is an oracle reserved keyword, so it is not wise to use it as a column name.
If you have no control of the table I think you may be able to surround it in double quotes eg select "COST" to avoid oracle picking it up as a reserved word.
By default Oracle creates fields in uppercase so the field name will need to be in uppercase unless when the table was created it was forced into different case by surrounding it in Quotes.
Check that you don't have invisible characters in your file and that you're using the right encoding. I sometimes accidentally introduce them because I have a non english keyboard map and accidentally hit the wrong key combination.
Just type again one of your SQL statements and test them.

Periods in table names causing errors

I am trying to update a table in pgadmin 3 (postgres 9.4) called: assay.Luminex.Luminex_GT_shared.Analyte
I kept getting the error:
ERROR: improper qualified name (too many dotted names):
assay.luminex.luminex_gt_shared.analyte
I have tested backquotes and brackets to no avail, keep getting syntax errors:
SELECT * FROM `assay.Luminex.Luminex_GT_shared.Analyte`;
SELECT * FROM [assay.Luminex.Luminex_GT_shared.Analyte];
I am a newb to SQL, I am just trying to make a simple update to a table and getting stuck on this.
As documented in the manual in SQL, non-standard identifiers have to be quoted with double quotes:
SELECT *
FROM "assay.Luminex.Luminex_GT_shared.Analyte";
But you should really avoid names like that.

Select column of other row in parameterized statement

I am a beginner in SQL, and I was having some trouble with special characters like parentheses and asterisks in user generated data. So far, I have mostly been using a lot of ad hoc methods of getting rid of these characters and they work well enough. Based on what I have read, I think paramaterized queries might be a more systematic way of getting around some of the problems that I have.
I have following query:
insert into midstep (street)(select street from addresses limit 10)
The column street in the table addresses has a lot of parantheses, asterisks etc. The code above works as expected. What I want to do is something like this:
prepare midstreet (text) AS insert into midstep (street)(select $1 from addresses limit 10);
execute midstreet ( street from addresses);
However, when I enter in that code I get the following error message:
ERROR: syntax error at or near "from"
SQL state: 42601
Character: 29
I have tried a bunch of variations on this code and read through the documentation on Prepare and Execute but always get error messages. Any help is appreciated!
EDIT: I forgot to mention, I am using postgresql 9.3 on and my os is Ubuntu. Please ask if you need any more information to help!
You do not need a parameter in your case, as you get your values using a sub-query. As a matter of fact, you cannot know the needed values on server-side. Parameters like that are needed if and only if you have some values defined on application level and you want to pass them to the database.

SQL query syntax error using INSERT INTO

So, I know my code for the database connection and reader is functional, because it has worked for me many times before, however, something about this SQL query:
gives this error message:
when this data is inputted:
This is the database table that I am trying to add the data to:
The issue is that you are using "password" as a column name and that's a reserved word in Jet SQL. Either change the name or escape it in SQL code. You do the latter by wrapping it in square brackets [].