Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column Name beginning with a number?

Tags:

sql

oracle

I have a column name in one of my tables called: 3RD_DIAG_CODE - VARCHAR2 (10 Byte)

When I try to run a query, it gives me the following error highlighting 3RD_DIAG_CODE.

ORA-00923: FROM keyword not found where expected.

How can I bring this field in without it throwing an error every time I bring this field in?

like image 201
JK0124 Avatar asked May 24 '11 17:05

JK0124


People also ask

Can database column name start with number?

The rules for naming database objects (such as tables, columns, views, and database procedures) are as follows: Names can contain only alphanumeric characters and must begin with an alphabetic character or an underscore (_).

Can SQL column start with number?

No, SQL Server does not allow to create the column names that starts with number.

Can MySQL column names start with a number?

FYI: MySQL does allow for identifiers to start with a decimal digit. Please see my answer (towards the bottom) where I address this. It is valid to create a table and/or column as just 4aii , without delimiters, in MySQL.

Can table names start with a number?

Table names shouldn't start with a number. They fall into the category of identifiers which , per Books Online, must conform to the following: The rules for the format of regular identifiers depend on the database compatibility level.


3 Answers

If you are using column names that start with a number then you need to use double quotes. For example:

create table foo (
"3RD_DIAG_CODE" varchar2(10 byte) --make sure you use uppercase for variable name
);

insert into foo values ('abc');
insert into foo values ('def');
insert into foo values ('ghi');
insert into foo values ('jkl');
insert into foo values ('mno');
commit;

select * from foo;

3RD_DIAG_C
----------
abc
def
ghi
jkl
mno

select 3RD_DIAG_CODE from foo;

RD_DIAG_CODE
------------
       3
       3
       3
       3
       3

select "3RD_DIAG_CODE" from foo;

3RD_DIAG_C
----------
abc
def
ghi
jkl
mno

Edit: As for the error message itself, you are probably (as BQ wrote) missing a comma from the select clause.

like image 161
gsiems Avatar answered Sep 23 '22 09:09

gsiems


Check your specification, but in SQL Server we would have to enclose that column name in square brackets: [3RD_DIAG_CODE]

like image 27
AllenG Avatar answered Sep 24 '22 09:09

AllenG


You probably have two columns listed without a comma between them.

create table t (id number primary key, 3d varchar2(30))
Error at Command Line:1 Column:39
Error report:
SQL Error: ORA-00904: : invalid identifier
00904. 00000 -  "%s: invalid identifier"


create table t (id number primary key, "3d" varchar2(30));
table T created.
desc t
Name Null     Type         
---- -------- ------------ 
ID   NOT NULL NUMBER       
3d            VARCHAR2(30) 


> select id, 3d from t --[as @gsiem mentions: THIS IS BAD]
ID                     3D       
---------------------- -------- 

> select id, "3d" from t
ID                     3d                             
---------------------- ------------------------------ 

> select id, [3d] from t

Error starting at line 7 in command:
select id, [3d] from t
Error at Command Line:7 Column:11
Error report:
SQL Error: ORA-00936: missing expression
00936. 00000 -  "missing expression"
*Cause:    
*Action:
> select id 3d from t

Error starting at line 8 in command:
select id 3d from t
Error at Command Line:8 Column:10
Error report:
SQL Error: ORA-00923: FROM keyword not found where expected
00923. 00000 -  "FROM keyword not found where expected"
*Cause:    
*Action:
like image 34
BQ. Avatar answered Sep 25 '22 09:09

BQ.