Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written the below method which inserts data into the database via a store procedure. It stores datetime in the format : YY/MM/DD HH:MM:SS:00 but i want to remove :SS part from it and want to store it like this: YY/MM/DD HH:MM. I tried many things, but dint help. Please help

CODE:

C#
public bool InsertCoordinates(string param)
    {

        String[] parts = param.Split(',');
        sqlCom.CommandText = "InsertCoordinates";
        sqlCom.CommandType = CommandType.StoredProcedure;
        bool result ;

        sqlCom.Parameters.Add("@AddedDateTime", SqlDbType.VarChar).Value = Convert.ToDateTime(parts[0].ToString());
        sqlCom.Parameters.Add("@RecordedDateTime", SqlDbType.VarChar).Value = Convert.ToDateTime(parts[2].ToString());


        try
        {
            sqlCon.Open();
            sqlCom.ExecuteNonQuery();
            result = Convert.ToBoolean(sqlParam.Value);
            return result;
        }



how ?
Posted
Updated 16-Apr-14 0:34am
v2
Comments
Emre Ataseven 16-Apr-14 6:58am    
Do you want to make second part 00?

Never store Date information in a database in string format - always user SQL DATETIME fields, and pass the C# DateTime value directly without converting it.

String stored dates are a PITA to work with: they are difficult to compare, difficult to do math with.
DateTime values can be used much more easily, and formatted exactly as you need when they are retrieved for display.
 
Share this answer
 
Comments
Mike Meinz 16-Apr-14 7:18am    
+5
A timeless answer to a perpetual question!
if you are using storeProcedure for saving data in your database then why can't you use this


SQL
select  CAST('07-25-08 12:35:29.123' AS smalldatetime) AS
        'smalldatetime'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900