Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ALTER TABLE help 1

Status
Not open for further replies.

welshone

Programmer
Jul 30, 2001
414
GB
hello,
how can I alter a field in a table to have a default value ?

I am trying this :


ALTER TABLE tblchat
ADD thetime DEFAULT =time()


not working.

I am using Access2000, but wantto beable to do this via SQL, instead of using the gui,

thanks for any help.
Jamie
 
This is how I've done this with SQL 7. Hope it helps.


alter table tblchat add thetime smalldatetime default getdate()
 
There is no time() [yet!] in SQL Server. We have the getdate() function, which returns both the current date and current time.

We also have a datetime and smalldatetime datatypes, which hold both a date and time together in one field. You can't have just date or time; must be both. (Even if you only specify time, SQL will store a default date in there anyway.)

So, if you want the 'thetime' column to hold both date and time, then jester777's solution will work. I usually like to add the NULL/NOT NULL option, so that I'm always clear on whether a column is allowed to have nulls or not.

alter table tblChat
add thetime datetime
NOT NULL default getdate()

If you really don't need/want the date, you could store the time in a char or varchar column (instead of datetime), and then convert getdate() to just a string containing the time only.

alter table tblChat
add thetime varchar(16) NOT NULL
default convert(varchar(16),getdate(),114)

See BOL for the various conversion options. '114' is one optioon that return time only.
 
If you want straight Access answers post your question in an Access forum. I recommend forum701 "Microsoft: Access Queries and JET SQL." Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
tbroadbent:
I haven't worked in Access (well, I did a few times at gunpoint, but that's it), so I find I am unable to visualize what might be going on behind the original question. What's your guess of the possible scenario here?
 
Welshone posted, "I am using Access2000, but want to be able to do this via SQL, instead of using the gui,..."

I assume Welshone refers to Jet SQL and the Access GUI. Forum701 deals with Jet SQL. Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
okay. I think I was thrown off by the suggestion to post in the Access forum. He's got something in Access, but wants to develop it/convert it/migrate it to SQL. I was just a little confused. Often happens.
 
sorry, I did mean access2000 not sql server.
but nevermind, I have decided to insert the value when a row is added instead of making a default value.

thanks for all the info. though.
 
I think you mean something like this:
ALTER TABLE dbo.ErrorLog ADD CONSTRAINT
DF_ErrorLog_ErrDateTime DEFAULT getdate() FOR ErrDateTime

where you already have a field, but just want to add a default value for it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top