Home > DeveloperSection > Forums > Insert date and time in European format into SQL Server from C#?
Babe Zaharias
Babe Zaharias

Total Post:19

Points:133
Posted on    July-15-2013 8:38 AM

 MSSQL Server MSSQL Server 
Ratings:


 1 Reply(s)
 2252  View(s)
Rate this:
Hi Developers!

Here's my query:

string sql = @"INSERT INTO MY_TABLE(DATE)

                            VALUES (convert(datetime, '{0}')";

 

sql = string.Format(sql, myDate);

myDate is a C# DateTime object and has this value before the string.format:

enter image description here

MY_TABLE has a DATE column as a DateTime type.

I got this error:

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

I need to store the myDatevalue in a format like: 31/12/2013 23:59:00 into MY_TABLE.

thanks in Advance



shreesh chandra shukla
shreesh chandra shukla

Total Post:105

Points:735
Posted on    July-16-2013 12:02 AM

Hello!

better to use sql parameters

string sql = @"INSERT INTO MY_TABLE(DATE) 
                            VALUES (@DATE)";

add the parameter to your SqlCommand

command.Parameters.AddWithValue("@DATE", myDate);

Don't want to miss updates? Please click the below button!

Follow MindStick