2

I am doing a project in school, I have to create a website tool for salesmen to fill what they have done during the day, i.e. amount of quotes, quote sum, orders, order sum etc. I am using Visual Studio 2010, ASP.NET with C# with a SQL database.

I have to create a table with different columns, that I know how. But what I need is to have a column called Date and it has the datatype date. I need it to be filled automatically without having to input it manually. The same date that the new information was added. I have searched for solution in google and other places but I think I am searching with the wrong keywords, hopefully you can help me.

The format I wish for the date to be is DD-MM-YYYY

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1781366
  • 33
  • 1
  • 4

4 Answers4

4

When you look for SQL default date on Google, the second result you get is this one.

In there, you have a default date example:

CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
OrderDate date DEFAULT GETDATE()
)

using the DEFAULT keyword.

alestanis
  • 21,519
  • 4
  • 48
  • 67
2

Create a sql datetime column in the database, and specify a default value of GetDate() or GetUtcDate() depending on which you want. Format is irrelevant on the input side; you will have to use a formatter on the select side (or in your c# code).

theMayer
  • 15,456
  • 7
  • 58
  • 90
  • Thank you very much this was exactly what I was looking for!!! You sir are the best one milion thanks to you! – user1781366 Oct 28 '12 at 20:28
  • You can also use [`CURRENT_TIMESTAMP`](http://msdn.microsoft.com/en-us/library/ms188751.aspx). – Bridge Oct 28 '12 at 21:12
0

You can set the default value for the column as current date time..

 create table tblname (
    fieldname datetime default getdate()
    )

Also see this question Add default value of datetime field in SQL Server to a timestamp

Community
  • 1
  • 1
Amitd
  • 4,769
  • 8
  • 56
  • 82
0

You can use one of this to insert in the table instead.

  • String s = System.DateTime.Now.ToString("dd.MM.yyyy");
  • DateTime now = System.DateTime.Now;

The second one would be your choice because the type specified in yur table is Date. If don't want to be setting it from the app, specify which database you are using to get a specific answer.

Igor
  • 1,532
  • 4
  • 23
  • 44