Home All Groups Group Topic Archive Search About

date format in parameter

Author
28 Jan 2005 10:07 PM
Paulb1us@newsgroup.nospam

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

Author
28 Jan 2005 10:27 PM
Miha Markic [MVP C#]
Why do you use NVarChar instead of DateTime?
You'll avoid this kind of problems..

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com

<Paulb1us@newsgroup.nospam> wrote in message
Show quoteHide quote
news:E9972E39-7E45-4751-A50F-D654D46C6DF6@microsoft.com...
>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
>
>
>
>
Are all your drivers up to date? click for free checkup

Author
28 Jan 2005 11:58 PM
W.G. Ryan eMVP
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.

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
<Paulb1us@newsgroup.nospam> wrote in message
Show quoteHide quote
news:E9972E39-7E45-4751-A50F-D654D46C6DF6@microsoft.com...
> 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
>
>
>
>
Author
29 Jan 2005 1:32 AM
Kevin Yu [MSFT]
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."
Author
30 Jan 2005 6:37 PM
Paulb1us@newsgroup.nospam
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.

Show quoteHide quote
"Kevin Yu [MSFT]" wrote:

> 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."
>
>
Author
1 Feb 2005 2:22 AM
Kevin Yu [MSFT]
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."

Bookmark and Share