date format in parameter

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have SQL parameter that looks like this:

cmdUpdate.Parameters.Add
("@invoicedate", SqlDbType.NVarChar, 40).Value = dr["invoice
date"].ToString();

IT is updating a field in a SQL2K database.
The database field is of type char and not date.

The data in dr["invoice date"] looks like this:

12/29/2004

But when the field is updated the database looks like this.

2/4/2004 12:00:00 AM .

How do I get it formated correctly.

Thanks

Paul
 
Why do you use NVarChar instead of DateTime?
You'll avoid this kind of problems..
 
Check the type of dr["invoice date"] locally and make sure it's not a
DateTime - if it is I think ShortDateString() instead of ToString() should
work.

BTW, I totally agree with Miha here - you really don't get any benefit by
using NVarchar for dates and in all likelihood- you'll need to sort on them
sometime in the future and you'll just have to cast everything again.
 
Hi Paul,

I agree with William to use DateTime.ToShortDateString method to achieve
this. We can first cast the field value to a DateTime object and then call
ToShortDateString like:

DateTime dt = (DateTime)dr["invoice date"];
cmdUpdate.Parameters.Add("@invoicedate", SqlDbType.NVarChar, 40).Value =
dt.ToShortDateString();

Of course using a DateTime field in database is a better choice as Miha
suggested.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thank you Kevin,

That works great.

I agree it should be DateTime Filed in the DB, all of my other date fields
are just that. But in this paticular instance the origianl design of the
schema won't allow it.
 
You're welcome, Paul.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top