January 23, 2012 at 10:54 am
I have a table that has two columns. 1. Name 2. CurrentDate
Table :-
Name Varchar (30)
Current Date datetime
The “Name” column is “ Not null” and the “Current date” column is Null
In the front end the application the user will only enter the Name and will not enter the information in the Current date column but it should take the current date and time automatically when the data was entered. Not sure how to achieve this. …Do we have to write a trigger or can we create a datatype which will get the current date and time automatically
January 23, 2012 at 11:00 am
You will need to make the DateTime column be not null and add a default constraint for that column of GetDate()
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
January 23, 2012 at 11:08 am
adding default value on existing column.
January 23, 2012 at 11:16 am
Alter table dbo.MyTable
add constraint DF_CurrentDate default(getdate()) on CurrentDate;
If I'm not mistaken, that should do what you need.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
January 23, 2012 at 3:03 pm
Thank you so much..that is exactly what i was looking for !!!! the only question i have is, Will that work on a null column ?
January 23, 2012 at 3:05 pm
It will work on a nullable column. Meaning that if you don't supply a value for that column it will use the default. You can also include that column in the insert list and supply an explicit null. Your existing data can maintain it's current null.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
January 24, 2012 at 2:09 pm
Thank you so much for your response and the explaination. That helped me alot
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply