473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SELECT problem when column name contains [ ] characters

Hi All,

I have run into another problem that is eating my lunch. Should be
simple but I am having one heck of a time. Please look at this SELECT
statement:

SELECT [StateName] FROM States WHERE [[2DigitCountryCode]] = "US";

[2DigitCountryCode] is the SQL column name (because it sarts with 2?).
As the statement is shown I get the error message: [Microsoft][ODBC SQL
Server Driver][SQL Server]Unclosed quotation mark before the character
string '[2DigitCountryCode] = "US"'. What quotation marks is this
talking about?

I have gotten the SELECT statement to run, without error message, when
I change it to:

SELECT [StateName] FROM States WHERE '[[2DigitCountryCode]]' = "US";

But, this statement returns nothing. I know there are records to
return so I did something wrong. I've tried () and {} but problem
remains.

I am coding an ASP page using VBScript, connecting to an SQL 2000
server via ADO. I have tried so many things but nothing works for me.
My head hurts....

Can anyone see what I have done wrong? SQL Books Online was not much
help to me and I could not find any previous posts regarding my
problem.
Thanks for any help,

Charles

Sep 18 '05 #1
7 11886
Ch*******@MailandNews.Com wrote:
SELECT [StateName] FROM States WHERE [[2DigitCountryCode]] = "US";

[2DigitCountryCode] is the SQL column name (because it sarts with 2?).
As the statement is shown I get the error message: [Microsoft][ODBC
SQL Server Driver][SQL Server]Unclosed quotation mark before the
character string '[2DigitCountryCode] = "US"'. What quotation marks
is this talking about?


It seems like you think brackets are required around column names. They
usually are not. SQL Server requires them when the column name makes parsing
ambiguous (when the column name is a SQL keyword or contains spaces, for
example). This is true not just of columns, but any user-named objects.

So it is incorrect to claim that [2DigitCountryCode] is the column name. The
brackets are there for the parser. The column name is 2DigitCountryCode.

Short answer: change your select statement to:

SELECT StateName FROM States WHERE [2DigitCountryCode] = 'US'

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Sep 18 '05 #2
Hi,

My project started out as a Access database. I defined all tables,
indexes and relationships before I used the 'Upsizing Wizard' to
transfer the whole database over to SQL (in my case MSDE SP3). The
wizard placed brackets around every field, in every table, that started
with a number or had the - character in it. When I look at the table
definitions the brackets are there, part of the field name.

I had already tried leaving the 2nd set of brackets off and the error
message I get is that the 2DigitCountyCode (notice no brackets) field
was not found. Are you saying I should remove these brackets from
every table that has fields with them?

True, I do not know a lot about SQL but I have read that brackets are
required for every field that has numbers or special characters (the
minus sign, in my case).

Should I remove the brackets from every table? I have created a 'View'
in SQL and I have gotten the SELECT to return the correct records.
But, it does not have a 'View SQL' menu item so I can not see what the
actual SQL query is.
Thanks for any help,

Charles

Sep 18 '05 #3
Hi,

As a test I have tried to remove the brackets from the
2DigitCountryCode field. Once I leave the field they are automatically
replaced. Seems these brackets are required in the SQL database but
not in the Access version of the database.

SELECT StateName FROM States WHERE [2DigitCountryCode] = 'US'

Works fine when used on the Access version of the database. If fails
when used on the SQL version of the database (field not found error
message).

I found a speaker icon that points up in the SQL view dialog. When I
hover my mose over it I get this: CRITERIA: {[2DigitCountryCode] =
N'US'}
I have tried using that statement for my WHERE clause but my SELECT
query still errors out.

I'm still scratching my head.
Thanks for any help,

Charles

Sep 18 '05 #4
Ch*******@MailandNews.Com wrote:
My project started out as a Access database. I defined all tables,
indexes and relationships before I used the 'Upsizing Wizard' to
transfer the whole database over to SQL (in my case MSDE SP3). The
wizard placed brackets around every field, in every table, that
started with a number or had the - character in it. When I look at
the table definitions the brackets are there, part of the field name.


When you say "look at the table definitions", do you mean you are looking in
Enterprise Manager? If that is the case, just use Enterprise Manager to
create a view with one of your tables and examine the SQL that is created.
This ought to clear thing up quite a bit.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Sep 18 '05 #5
Hi,

I do not have Enterprise Manager. I am using Access XP to view the
tables and field names. I used the 'Create view in designer' option to
create my SQL view. It does work as it should here, on my computer,
but I can not see the actual SQL statement. I am trying to figure out
the actual SQL query statement so I can use it on the SQL 2000 server
on my web site. I use ADO and VBScript (ASP) to access the SQL server.

The error message I posted in my first post is the actual message
returned from SQL 2000 using ADO.
Thanks for your time,

Charles

Sep 18 '05 #6
Ch*******@MailandNews.Com wrote:
Hi,

My project started out as a Access database. I defined all tables,
indexes and relationships before I used the 'Upsizing Wizard' to
transfer the whole database over to SQL (in my case MSDE SP3). The
wizard placed brackets around every field, in every table, that
started with a number or had the - character in it.
No, it could not have made a bracket be part of the column name. See below
as well as the article cited in my footnote. *
When I look at
the table definitions the brackets are there, part of the field name.

The tool you are using to view these names is putting the brackets there.
I had already tried leaving the 2nd set of brackets off and the error
message I get is that the 2DigitCountyCode (notice no brackets) field
Do you mean that you did this?:
....where [2DigitCountyCode] ...
was not found. If this column name was not found when you used a single set of brackets,
then I would suggest doing this:

EXEC sp_columns @table_name='States'

to find out the real names of your columns.
Regarding "2DigitCountyCode": this name is not a legal identifier:

The first character must be one of the following:
a.. A letter as defined by the Unicode Standard 2.0. The Unicode
definition of letters includes Latin characters from a through z and from A
through Z, in addition to letter characters from other languages.

This means that if you can't change the name of the field (which I highly
recommend that you do), then you must surround the name with brackets
whenever you use it in a sql statement ... one set of brackets.
Are you saying I should remove these brackets from
every table that has fields with them?

You can't: they aren't there*.
The tool you are using to view these names is putting the brackets there. True, I do not know a lot about SQL but I have read that brackets are
required for every field that has numbers or special characters (the
minus sign, in my case).
You are almost correct. Amend your sentence to say:
"When using names containing numbers or special characters in SQL
statements, you must surround those names with brackets in order to prevent
the query parser from generating an error."

Should I remove the brackets from every table?
You can't. they aren't there. The tool you are using to view these names is
putting the brackets there.
I have created a
'View' in SQL and I have gotten the SELECT to return the correct
records. But, it does not have a 'View SQL' menu item so I can not
see what the actual SQL query is.


How did you create the view? Did you use Access? When generating SQL, Access
will always put brackets around object identifiers whether they are needed
or not.

You can see the statement by querying the INFORMATION_SCHEMA.VIEWS view:

SELECT
t.VIEW_DEFINITION
FROM INFORMATION_SCHEMA.VIEWS t WHERE
TABLE_CATALOG = 'YourDatabaseName' AND
TABLE_NAME = 'YourViewName'
HTH,
Bob Barrows

* Article in BOL called "Using Identifiers":
mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%2 0Server\80\Tools\Books\acdata.chm::/ac_8_con_03_6e9e.htm
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Sep 18 '05 #7
Hi,
When I look at
the table definitions the brackets are there, part of the field name.
The tool you are using to view these names is putting the brackets
there.
I had already tried leaving the 2nd set of brackets off and the error
message I get is that the 2DigitCountyCode (notice no brackets) field
Do you mean that you did this?:
....where [2DigitCountyCode] ...

Yes, that is exactly what I did.
I have created a
'View' in SQL and I have gotten the SELECT to return the correct
records. But, it does not have a 'View SQL' menu item so I can not
see what the actual SQL query is.


How did you create the view? Did you use Access? When generating SQL,
Access
will always put brackets around object identifiers whether they are
needed
or not.

Yes, I used Access XP to create the SQL view.

You can see the statement by querying the INFORMATION_SCHEMA.VIEWS
view:

SELECT
t.VIEW_DEFINITION
FROM INFORMATION_SCHEMA.VIEWS t WHERE
TABLE_CATALOG = 'YourDatabaseName' AND
TABLE_NAME = 'YourViewName'

That is all greek to me, sorry to say. I will have to read the links
you pointed to and see if it will help me understand.

To solve the problem I have renamed all the affected field names. I
did not know I have done something illegal. Seems this is the only
correct way to fix this problem.
Thanks, everyone, for the input,

Charles

Sep 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Martin Lucas-Smith | last post by:
Can anyone point me to a regular expression in PHP which could be used to check that a proposed (My)SQL database/table/column name is valid, i.e. shouldn't result in an SQL error when created? ...
1
by: Manu | last post by:
Hi, ftplib fails to cwd into a directory which contains spaces in it's name.How do i correct this? Regards Manu
3
by: Steven de Vries | last post by:
Hi, I'am trying a very simple sql statement but it does not work. I use the SQL version 8.0 I use the "Northwind" sample database and the Table "Employees". The sql statement is: SELECT * ...
10
by: Colleyville Alan | last post by:
I am trying to turn a short and fat (63 columns) table into one that is tall and skinny (7 columns). Basically, I am trying to create a "reverse crosstab" using a looping structure in VBA along...
1
by: mirela | last post by:
Hello, I'm using RedHat 9 and PostgreSQL 7.3.4. I have a table with a column of type varchar and length 250. This column includes both English and Hebrew text values. The following select...
1
by: imauser | last post by:
I have a database(PostgreSQL) table(about 70k rows).I am developing an ASP webpage and there is a list-box on it which contains the name of the columns of that table. User selects the column name...
5
by: Chris Botha | last post by:
There is a database table with one of the columns named "System". When creating a new Dataset for this table, the project gives a huge number of compile errors. If I change the column name to be...
1
by: thesti | last post by:
hi, how to reference an alias column name that consists more than one word. it's like this select address, substring(address, 5, 8) as 'member's address' from msmember where...
0
by: buttslapper | last post by:
Hi, I'm not sure it's a .net problem or what, so. I'm using an OleDbDataAdapter to fill a dataset from an Excel worksheet. When my column name contains a dot ".", the dot is replaced with a #...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.