473,748 Members | 10,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 [[2DigitCountryCo de]] = "US";

[2DigitCountryCo de] 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 '[2DigitCountryCo de] = "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 '[[2DigitCountryCo de]]' = "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 11912
Ch*******@Maila ndNews.Com wrote:
SELECT [StateName] FROM States WHERE [[2DigitCountryCo de]] = "US";

[2DigitCountryCo de] 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 '[2DigitCountryCo de] = "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 [2DigitCountryCo de] is the column name. The
brackets are there for the parser. The column name is 2DigitCountryCo de.

Short answer: change your select statement to:

SELECT StateName FROM States WHERE [2DigitCountryCo de] = '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 2DigitCountyCod e (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
2DigitCountryCo de 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 [2DigitCountryCo de] = '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: {[2DigitCountryCo de] =
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*******@Maila ndNews.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*******@Maila ndNews.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 2DigitCountyCod e (notice no brackets) field
Do you mean that you did this?:
....where [2DigitCountyCod e] ...
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='St ates'

to find out the real names of your columns.
Regarding "2DigitCountyCo de": 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_SCH EMA.VIEWS view:

SELECT
t.VIEW_DEFINITI ON
FROM INFORMATION_SCH EMA.VIEWS t WHERE
TABLE_CATALOG = 'YourDatabaseNa me' AND
TABLE_NAME = 'YourViewName'
HTH,
Bob Barrows

* Article in BOL called "Using Identifiers":
mk:@MSITStore:C :\Program%20Fil es\Microsoft%20 SQL%20Server\80 \Tools\Books\ac data.chm::/ac_8_con_03_6e9 e.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 2DigitCountyCod e (notice no brackets) field
Do you mean that you did this?:
....where [2DigitCountyCod e] ...

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_SCH EMA.VIEWS
view:

SELECT
t.VIEW_DEFINITI ON
FROM INFORMATION_SCH EMA.VIEWS t WHERE
TABLE_CATALOG = 'YourDatabaseNa me' 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
17351
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? The user of my (hopefully to be opensourced) program has the ability to create database/table/column names on the fly. I'm aware of obvious characters such as ., , things like >, etc., which won't work, but haven't been able to source a...
1
2381
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
56588
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 * From Employees Where City = "London"
10
6562
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 with SQL. I'd like to take the name of the column and input it into a descritor field. This isn't the table, but will serve as a better illustration than the real deal. If the table looks like this:
1
1692
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 statements work fine: select * from table where column='English value'; select * from table where column like 'Hebrew value';
1
4561
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 from that box and a query is sent to the database. The data from that column should be returned to the user. Now the problem is that, I have seen the 'SELECT' statements with the exact column name written in it.But in my case the column name is...
5
1662
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 anything else than "System" and re-generate the Dataset, it works. The problem is that this is an existing database and there is no way that the column name will be changed.
1
2018
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 charindex('r', substring( alias column name, 1, 6))>0 i want to check if the first 6 chars of 'member's address' contains 'r'.
0
1028
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 # in the DataColumn.ColumnName. Does anyone can help me with this issue ?
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9370
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.