Missing comma and Column alias

When you write SELECT statement and use column alias always use AS followed by Alias name. If you miss out a comma between column names,  you will get different result sets with different column names

Consider the flowing set of code

create table #employee_master(emp_id int, emp_name varchar(100), dob varchar(12))
insert into #employee_master
elect 1,'Sankar','09/19/1976' union all
select 2,'Kamal','02/11/1968' union all
select 3,'Rajesh','22/29/1200'
Select emp_id, emp_name dob from #employee_master

Note that there are three columns in the SELECT statement and there is a missing comma between emp_name and dob. If you execute the above code, the result is

emp_id         dob
----------- -------------
1              Sankar
2              Kamal
3              Rajesh

If you look at the result , there are only two columns and under the column dob the names are displayed. Because in the SELECT statement emp_name dob is actually considered as emp_name as dob and dob becomes the alias name for emp_name.

You should be aware of this. If your SELECT statement has many column names always make sure there is no missing comma. If there is, make sure if it is a proper alias name and use AS between column names


One thought on “Missing comma and Column alias

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s