Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. A dash in an sql query

A dash in an sql query

Scheduled Pinned Locked Moved C#
databasequestioncsharpsql-serversysadmin
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yevgeny Efter
    wrote on last edited by
    #1

    Hello! I am writing an application that works with a SQL Server database, and in the application I try to run a query which has a dash ('-') in it. Because of the dash, the query doesn't work. I guess that the .net compiler understands the dash as a special character, and sends the wrong query to SQL Server. Is it right? Or the problem is not this? How can I solve it? Thanks!

    G C 2 Replies Last reply
    0
    • Y Yevgeny Efter

      Hello! I am writing an application that works with a SQL Server database, and in the application I try to run a query which has a dash ('-') in it. Because of the dash, the query doesn't work. I guess that the .net compiler understands the dash as a special character, and sends the wrong query to SQL Server. Is it right? Or the problem is not this? How can I solve it? Thanks!

      G Offline
      G Offline
      Gareth H
      wrote on last edited by
      #2

      Yevgeny Efter, Post the SQL and maybe we can see why its not working. Regards, Gareth.

      1 Reply Last reply
      0
      • Y Yevgeny Efter

        Hello! I am writing an application that works with a SQL Server database, and in the application I try to run a query which has a dash ('-') in it. Because of the dash, the query doesn't work. I guess that the .net compiler understands the dash as a special character, and sends the wrong query to SQL Server. Is it right? Or the problem is not this? How can I solve it? Thanks!

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        Yevgeny Efter wrote:

        a query which has a dash ('-') in it. Because of the dash, the query doesn't work. I guess that the .net compiler understands the dash as a special character, and sends the wrong query to SQL Server. Is it right? Or the problem is not this?

        The C# compiler (.NET is the framework) will not change your query. So, no, that is not right. Most likely the SQL code supplied is not legal SQL in the first place.

        Yevgeny Efter wrote:

        How can I solve it?

        Since we have no idea of what you have actually written it is difficult to say. There are a number of possibilities but they all depend on where this dash is and why it is there. For example, is the dash in a column or table name? If so then escape it with square brackets e.g. [my-column-name-with-dashes]

        Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Mixins in C#3.0 My website | Blog

        Y 1 Reply Last reply
        0
        • C Colin Angus Mackay

          Yevgeny Efter wrote:

          a query which has a dash ('-') in it. Because of the dash, the query doesn't work. I guess that the .net compiler understands the dash as a special character, and sends the wrong query to SQL Server. Is it right? Or the problem is not this?

          The C# compiler (.NET is the framework) will not change your query. So, no, that is not right. Most likely the SQL code supplied is not legal SQL in the first place.

          Yevgeny Efter wrote:

          How can I solve it?

          Since we have no idea of what you have actually written it is difficult to say. There are a number of possibilities but they all depend on where this dash is and why it is there. For example, is the dash in a column or table name? If so then escape it with square brackets e.g. [my-column-name-with-dashes]

          Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Mixins in C#3.0 My website | Blog

          Y Offline
          Y Offline
          Yevgeny Efter
          wrote on last edited by
          #4

          Thank you! The brackets solved it.

          Have a nice day!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups