x

How do I add a new column with an apostrophe in the name?

I am trying to alter a table by adding a new column. New column name = today's date. I don't know how to code the apostrophe.

Trying ALTER TABLE tblname ADD today's date DATETIME
more ▼

asked Apr 18, 2012 at 02:16 AM in Default

Joni gravatar image

Joni
20 1 1 1

Two good answers, please be sure to mark which one you find most helpful.
Apr 18, 2012 at 08:56 AM Grant Fritchey ♦♦
(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

OK @Usman has shown you how to do it and @Magnus has explained the dangers in doing so, but I have to ask why you would want to store a value of today's date in a column? That does smell like a bad design.

What happens when I read the data entered yesterday, tomorrow?

Even if the table only ever contains "today's" data, name the column with something that reflects what the date means (e.g. EnteredDate, InsertedDate, LogDate, etc) - then you can see easily if the data has been refreshed.

If the data never can go stale, and is always "today's" then no need to store a date at all select cast(getdate() as date) will do.

more ▼

answered Apr 18, 2012 at 07:11 AM

Kev Riley gravatar image

Kev Riley ♦♦
53.2k 47 49 76

Absolutely great advice.
Apr 18, 2012 at 08:55 AM Grant Fritchey ♦♦
(comments are locked)
10|1200 characters needed characters left

There are couple of ways you can do this, but I prefer not to use spaces,single quotes etc. in the object names

Anyways, one way to do it is to put square brackets around the column name i.e.

ALTER TABLE tblname ADD [today's date] DATETIME

Another way is to have QUOTED_IDENTIFIER setting ON and double quote the column name i.e.

SET QUOTED_IDENTIFIER ON;
ALTER TABLE tblname ADD "today's date" DATETIME
more ▼

answered Apr 18, 2012 at 04:31 AM

Usman Butt gravatar image

Usman Butt
13.9k 6 8 14

I totally agree about not using single quotes or apostrophes in object names.

create table magnus.test (id int identity(1,1) primary key)
alter table magnus.test
add [today's date] datetime
alter table magnus.test
add [today´s date] datetime
alter table magnus.test
add [today`s date] datetime
The above is just one of the many reasons to avoid it :)
Apr 18, 2012 at 05:46 AM Magnus Ahlkvist
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x2

asked: Apr 18, 2012 at 02:16 AM

Seen: 1739 times

Last Updated: Apr 18, 2012 at 08:56 AM