Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to insert datetime into the SQL Database table?

How can I insert datetime into the SQL Database table ? Is there a way to insert this query through the insert command in C# / .NET?

like image 971
Srihari Avatar asked Mar 13 '11 04:03

Srihari


People also ask

How can I insert datetime table value in SQL?

insert into table1(approvaldate)values('20120618 10:34:09 AM'); If you are married to the dd-mm-yy hh:mm:ss xm format, you will need to use CONVERT with the specific style. insert into table1 (approvaldate) values (convert(datetime,'18-06-12 10:34:09 PM',5));

How do I insert date and timestamp in SQL?

Insert the date with the TO_DATE function. SQL> INSERT INTO table_dt VALUES(4, TO_DATE('01-JAN-2003', 'DD-MON-YYYY')); Display the data. Set the timestamp format.

How do you insert date and time in a table?

The TO_DATE function allows you to define the format of the date/time value. For example, we could insert the '3-may-03 21:02:44' value as follows: insert into table_name (date_field) values (TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss')); Learn more about the TO_DATE function.

How do you insert a date in a table?

In Oracle, for the date format provided in question, you can use to_date to convert your string date literal input along using format 'DD-MON-YYYY' to data type date.


2 Answers

DateTime values should be inserted as if they are strings surrounded by single quotes:

'20100301' 

SQL Server allows for many accepted date formats and it should be the case that most development libraries provide a series of classes or functions to insert datetime values properly. However, if you are doing it manually, it is important to distinguish the date format using DateFormat and to use generalized format:

Set DateFormat MDY --indicates the general format is Month Day Year  Insert Table( DateTImeCol ) Values( '2011-03-12' ) 

By setting the dateformat, SQL Server now assumes that my format is YYYY-MM-DD instead of YYYY-DD-MM.

SET DATEFORMAT

SQL Server also recognizes a generic format that is always interpreted the same way: YYYYMMDD e.g. 20110312.

If you are asking how to insert the current date and time using T-SQL, then I would recommend using the keyword CURRENT_TIMESTAMP. For example:

Insert Table( DateTimeCol ) Values( CURRENT_TIMESTAMP ) 
like image 94
Thomas Avatar answered Sep 22 '22 13:09

Thomas


You will need to have a datetime column in a table. Then you can do an insert like the following to insert the current date:

INSERT INTO MyTable (MyDate) Values (GetDate()) 

If it is not today's date then you should be able to use a string and specify the date format:

INSERT INTO MyTable (MyDate) Values (Convert(DateTime,'19820626',112)) --6/26/1982 

You do not always need to convert the string either, often you can just do something like:

INSERT INTO MyTable (MyDate) Values ('06/26/1982')  

And SQL Server will figure it out for you.

like image 28
Abe Miessler Avatar answered Sep 21 '22 13:09

Abe Miessler