Issue with SQL Query and the - character

Posted on 2009-04-22
Last Modified: 2012-05-06

I have a sql query in SQL 2005:
On one computer (MY-SQL itself), i recieve this error when running the query:
Incorrect syntax near '-'.

I have an application that runs on this server, that uses this query, which also gives me the same error.

I have the same application on another computer, that DOES NOT give me this error...

My question is ...WHY!!!

Question by:melegant99
    LVL 142

    Accepted Solution

    -  is invalid inside names... unless you put [] around it:
    select [MY-SQL].MYDATABASE.MYTABLE.idnum FROM etc.

    Open in new window


    Author Comment

    Yes, that worked great. But - It does work for me through my application that resides on any other computer other than the SQL server itself...and the code is NOT putting brackets around the name (although, that is what I'll do to fix it)
    LVL 57

    Expert Comment

    by:Raja Jegan R
    Your servers differ in the versions. Might be because of ANSI_DEFAULTS option

    To avoid obtaining errors in both your machines.

    select [MY-SQL].MYDATABASE.MYTABLE.idnum FROM etc

    - is a Subtraction string and since it is present within characters, you are obtaining this error. If you encapsulate it within "" or [] then it considers that as a single object and you wont be obtaining errors out of it. This will be checked based upon the Collation and ANSI_DEFAULTS option of your SQL Server.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now