By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,213 Members | 1,984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,213 IT Pros & Developers. It's quick & easy.

Convert DateTime.Now() to SQL Server(tm) standard internal format fordatetime

P: n/a
Hi,

How can I convert a value created from DateTime.Now() to the datetime
format that SQL Server recognises?
Thanks in advance.

Feb 6 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

Just like that :)

//If you are using parameters THE PREFERRED WAY
com.Parameters.Add("@adate", SqlDbType.DateTime).Value = DateTime.Now;

//building the text
com.CommandText = String.Format("update T set aDate = '{0}' ",
DateTime.Now.ToShortDateString() );

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
<hu********@gmail.comwrote in message
news:f9**********************************@s19g2000 prg.googlegroups.com...
Hi,

How can I convert a value created from DateTime.Now() to the datetime
format that SQL Server recognises?
Thanks in advance.

Feb 6 '08 #2

P: n/a
//building the text
com.CommandText = String.Format("update T set aDate = '{0}' ",
DateTime.Now.ToShortDateString() );
I know it was only presented as the second option, but just to note
that even in this case you are probably at risk if the client has a
different locale to the server; which is yet another reason to use
parameters (aimed at the OP).

Format aside, you may also (depending on the system) have to worry
about timezones ;-p
UTC is never a bad choice in such scenarios...

Marc
Feb 6 '08 #3

P: n/a
It's not guaranteed that the current thread culture is the one that the
server will use for processing.

Rather, if you MUST use a literal in a constructed string, you would use
the format:

yyyy-MM-dd HH:mm:ss.fff

This will represent down to milliseconds (which is the highest
resolution of the datetime data type on SQL Server). You can choose to omit
anything from one point in the string on (for example, everything from hour
(HH) on), depending on the comparison being made.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:uY**************@TK2MSFTNGP05.phx.gbl...
Hi,

Just like that :)

//If you are using parameters THE PREFERRED WAY
com.Parameters.Add("@adate", SqlDbType.DateTime).Value = DateTime.Now;

//building the text
com.CommandText = String.Format("update T set aDate = '{0}' ",
DateTime.Now.ToShortDateString() );

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
<hu********@gmail.comwrote in message
news:f9**********************************@s19g2000 prg.googlegroups.com...
>Hi,

How can I convert a value created from DateTime.Now() to the datetime
format that SQL Server recognises?
Thanks in advance.


Feb 6 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.