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?
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));
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.
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.
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.
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 )
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With