Skram
Skram

Reputation: 103

Send only date and not time to date field in sql

I'm trying to update a DATE (not DATETIME) column in SQL Server, but when I execute the SQL command, it is posted with time format also.

string datetosql = "10.4.2015";

            string sqlQuery = "UPDATE tbl_tasks " +
                                "SET description = '" + tbTaskDescription.Text + "', " +
                                "deadline = '" + datetosql + "' , " +
                                "status = '" + statusIndex.ToString() + "' " +
                                "WHERE tid = " + _TaskID;

When I later collect the date using SqlDataReader, it is posted with time format: 04.10.2015 00:00:00. Why is this happening?

Upvotes: 0

Views: 529

Answers (2)

Add a cast to datetosql, ex:

Cast(datetosql as Date) 

Upvotes: -1

Patrick Hofman
Patrick Hofman

Reputation: 156978

.NET doesn't have a date only data type.

If you ask to return a SQL Server date, or a time, it always returns a DateTime struct instance in .NET. For a date, the time properties (hours, minutes, etc.) will be 0.

Note that using queries that aren't parameterized is considered bad! You are open for SQL injection, and you make your life harder since you need to escape quotes, etc.

Upvotes: 4

Related Questions