?
Solved

Modifying a query to account for spaces in field names

Posted on 2012-04-04
7
Medium Priority
?
303 Views
Last Modified: 2012-04-06
I have a query borrowed from Jeff Smith for comparing 2 tables in SQL server, and I'm now trying to adapt it for field names in a table (that I don't control) which have spaces.  For example, let's say COL1 below was actually COL 1.  I have tried square brackets, but that doesn't seem to work.  Thank you.

SELECT Min(tmp.TableName) AS TableName, tmp.ID, tmp.COL1, tmp.COL2, tmp.COL3
FROM [SELECT 'Table A' as TableName, A.ID, A.COL1, A.COL2, A.COL3
  FROM TblA as A
  UNION ALL
  SELECT 'Table B' as TableName, B.ID, B.COL1, B.COl2, B.COL3
  FROM TblB as B
]. AS tmp
GROUP BY tmp.ID, tmp.COL1, tmp.COL2, tmp.COL3
HAVING (((Count(*))=1))
ORDER BY tmp.ID;
0
Comment
Question by:GIStewart
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 37808639
Did you try adding the brackets to the field names or a whole query?

This should work:

SELECT Min(tmp.TableName) AS TableName, [tmp.ID], [tmp.COL 1], [tmp.COL 2], [tmp.COL 3]
...
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37808640
you have to change the "[" to "(" on this part of the query  

FROM [SELECT  to FROM (SELECT

you have to change the "]." to ")" on this part

]. AS tmp    to   ) AS tmp

so your query will look like this


SELECT Min(tmp.TableName) AS TableName, tmp.ID, tmp.COL1, tmp.COL2, tmp.COL3
FROM (SELECT 'Table A' as TableName, A.ID, A.COL1, A.COL2, A.COL3
  FROM TblA as A
  UNION ALL
  SELECT 'Table B' as TableName, B.ID, B.COL1, B.COl2, B.COL3
  FROM TblB as B
) AS tmp
GROUP BY tmp.ID, tmp.COL1, tmp.COL2, tmp.COL3
HAVING (((Count(*))=1))
ORDER BY tmp.ID;


also, enclose fields with spaces in [ ]   like  [COL 1]
0
 

Author Comment

by:GIStewart
ID: 37814234
Neither option appeared to work. I am trying to do this in access, and I have attached my sample db.  

Query1 compares TblA and TblB, and seems to work fine.

Query2 compares TblC and TblD, and has the square brackets.  (TblC is the same as TblA, TblD is the same as TblB).  But when I try and run that query, I get an "Invalid bracketing of name....." message, without even adding any spaces in the field names.  I'm not sure if it's another bracketing issue as capricorn1 suggests.  Thank you.
EE-Compare-test.mdb
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37814347
this is the revision of your query  "query2"


SELECT Min(tmp.TableName) AS TableName, (tmp.ID), (tmp.COL1), (tmp.COL2), (tmp.COL3)
FROM (SELECT 'Table C' as TableName, (C.ID), (C.COL1), (C.COL2), (C.COL3)
  FROM TblC as C
  UNION ALL SELECT 'Table D' as TableName, (D.ID), (D.COL1), (D.COL2), (D.COL3)
  FROM TblD as D
) AS tmp
GROUP BY (tmp.ID), (tmp.COL1), (tmp.COL2), (tmp.COL3)
HAVING (((Count(*))=1))
ORDER BY (tmp.ID);


see query3
EE-Compare-test.mdb
0
 

Author Comment

by:GIStewart
ID: 37815792
thank you - so if Col1 was really Col 1 (i.e. with a space), how would I modify Query3?
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 1000 total points
ID: 37815817
if COL1 was really Col 1
you will enclose it in brackets, like this [COL 1]

this will be the format of the query

SELECT Min(tmp.TableName) AS TableName, (tmp.ID), (tmp.[COL 1]), (tmp.COL2), (tmp.COL3)
FROM (SELECT 'Table C' as TableName, (C.ID), (C.[COL 1]), (C.COL2), (C.COL3)
  FROM TblC as C
  UNION ALL SELECT 'Table D' as TableName, (D.ID), (D.[COL 1]), (D.COL2), (D.COL3)
  FROM TblD as D
) AS tmp
GROUP BY (tmp.ID), (tmp.[COL 1]), (tmp.COL2), (tmp.COL3)
HAVING (((Count(*))=1))
ORDER BY (tmp.ID);


or this, removing the unnecessary "("   and  ")"



SELECT Min(tmp.TableName) AS TableName, tmp.ID, tmp.[COL 1], tmp.COL2, tmp.COL3
FROM (SELECT 'Table C' as TableName, C.ID, C.[COL 1], C.COL2, C.COL3
  FROM TblC as C
  UNION ALL SELECT 'Table D' as TableName, D.ID, D.[COL 1], D.COL2, D.COL3
  FROM TblD as D
) AS tmp
GROUP BY tmp.ID, tmp.[COL 1], tmp.COL2, tmp.COL3
HAVING (((Count(*))=1))
ORDER BY tmp.ID;
0
 

Author Comment

by:GIStewart
ID: 37817013
this works perfectly, thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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