sql fieldname

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geraldjr30
    New Member
    • Jan 2009
    • 60

    sql fieldname

    hi,

    i have the following:

    Code:
    $sql="SELECT 'field name here', field2, age...
    not sure how i would include a field name that contains spaces in the SELECT statement. I have an ODBC connection to MS Access..


    Thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    for this reason one usually does not use field names with spaces.... maybe escaping with a backslash, but I'm not sure

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      I think, but I'm not sure, you have to do it like this (if you have spaces in field names):

      Code:
      SELECT 
          `Column 1` AS Column_1
      FROM
          ....

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        I remember the Microsoft SQL server GUI thing using brackets around field names.
        Maybe that would work for Access as well?

        Like:
        [code=sql]SELECT [Field name] FROM [My table][/code]
        Last edited by Atli; Jan 20 '09, 04:28 PM. Reason: Changed the whole thing... was thinking of MySQL

        Comment

        • geraldjr30
          New Member
          • Jan 2009
          • 60

          #5
          hi,

          that 'field name' as field_name did not work.

          In the meantime, I am just changing the table column names so they dont include spaces. But if i could figure out how to avoid changing table column names that would be helpful for those who insist that they not change.

          thanks in advance,
          geebee

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Did you try the bracket thing I put in my last post?
            According to what I've read that should work.

            Comment

            • geraldjr30
              New Member
              • Jan 2009
              • 60

              #7
              thanks! the brackets worked.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Stupid me.. I overlooked the part about MS Access.

                -_-

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  As a side note: in MySQL 5.0.27, you can have field names with spaces in them and select them as you would any other field.

                  Code:
                  SELECT
                      `Field Column`
                  FROM
                      `table`

                  Comment

                  Working...