Insert Datetime Sql Server

 

How do I insert a datetime value into a SQL database table where the type of the column is datetime?

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.

Matthew Dresser
7,8357 gold badges64 silver badges108 bronze badges
sama

8 Answers

The following should work and is my recommendation (parameterized query):

Thorsten DittmarThorsten DittmarDatetime
48k4 gold badges73 silver badges121 bronze badges

and execute the query. DateTime.Now is to insert current Datetime.

Sean B
8,1542 gold badges21 silver badges34 bronze badges
ReshmaReshma
5301 gold badge14 silver badges33 bronze badges

It's more standard to use the format yyyy-mm-dd hh:mm:ss (IE: 2009-06-23 19:30:20)

Using that you won't have to worry about the format of the date (MM/DD/YYYY or DD/MM/YYYY). It will work with all of them.

AndreaAndrea
1,8512 gold badges13 silver badges13 bronze badges

It's been awhile since I wrote this stuff, so this may not be perfect. but the general idea is there.

Insert Date Sql Server

WARNING: this is unsanitized. You should use parameters to avoid injection attacks.

EDIT: Since Jon insists.

JoelJoel
17.2k2 gold badges54 silver badges81 bronze badges

This is an older question with a proper answer (please use parameterized queries) which I'd like to extend with some timezone discussion. For my current project I was interested in how do the datetime columns handle timezones and this question is the one I found.

Turns out, they do not, at all.

Sql

datetime column stores the given DateTime as is, without any conversion. It does not matter if the given datetime is UTC or local.

You can see for yourself:

What this will print will of course depend on your time zone but most importantly the read values will all have Kind = Unspecified. The first and second output line will be different by your timezone offset. Second and third will be the same. Using the 'o' format string (roundtrip) will not show any timezone specifiers for the read values.

Example output from GMT+02:00:

Also note of how the data gets truncated (or rounded) to what seems like 10ms.

joonasjoonas
Sql

you can send your DateTime value into SQL as a String with its special format. this format is 'yyyy-MM-dd HH:mm:ss'

Insert Datetime Sql Server Tutorial

Example: CurrentTime is a variable as datetime Type in SQL. And dt is a DateTime variable in .Net.

PedPakPedPak

Age of empires 1 sounds. Convert the existing DateTime object to a string with single quotes around it. I don't think it really matters what format it is, as long as it is valid.

Something like this:

Optionally you can create a new format and pass it as a parameter to data.ToString(format);

RomCoo
1,6062 gold badges16 silver badges27 bronze badges
DanDan

How To Insert Date In Sql Table

atfergsatfergs
1,5801 gold badge10 silver badges15 bronze badges
Copyright © 2019 nowbotwave