0

I have date in format 2013-08-05 12:45:56. But I want to store this in format 2013-08-05 00:00:00 in database.

The thing I want is, I don't want to save the time in database.

I am using:

cmd.Parameters.AddWithValue("@VoucherDate", VoucherDate.Date);

But still it is saving the time. Any suggestions?

RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
Fahad Murtaza
  • 256
  • 2
  • 9
  • 18

5 Answers5

2

If you're using SQL 2008 or above, then you can use the Date data type

http://msdn.microsoft.com/en-us/library/bb675168.aspx

If you want to get the Date part only in C#, then you can use the Date property e.g.

var fullDate = DateTime.Now;
var dateOnly = fullDate.Date;

But, if you're only looking to store the date portion, then consider changing the SQL datatype.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
1

Use the date column type instead of datetime

http://technet.microsoft.com/en-us/library/bb630352.aspx

Rand Random
  • 7,300
  • 10
  • 40
  • 88
0

Even you have Date, or DateTime column in database, when you read data back to your program you can format the display date by giving correct DataTime format string.

like dt.ToString("yyyy-MM-dd) , then you will not get any time as output

Damith
  • 62,401
  • 13
  • 102
  • 153
0

Once I too had a similar problem, with the help of this answer I fixed it.

Your C# code:

cmd.Parameters.AddWithValue("VoucherDate", VoucherDate.Date);

Tune your SQL query like below mentioned,

DATEADD(dd, 0, DATEDIFF(dd, 0, @VoucherDate))
Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164
-1

Try using:

VoucherDate.Date.ToShortDateString();
Daniele
  • 1,938
  • 16
  • 24
  • 1
    This is not an optimal solution and the reason for this is because dates should be stored as dates (which is actually a 3 byte number). This answer casts the date into a string which is just wrong for all purposes but presentation. Please see the answer from @christiandev instead. – Mikael Östberg Aug 05 '13 at 09:30
  • @Mikael Östberg You are right, i just showed a fast solution, the `ToShortDateString` should be used for presentation pourpouse as you stated – Daniele Aug 06 '13 at 07:45