PC Review


Reply
Thread Tools Rate Thread

date format in parameter

 
 
=?Utf-8?B?UGF1bGIxdXNAbmV3c2dyb3VwLm5vc3BhbQ==?=
Guest
Posts: n/a
 
      28th Jan 2005
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




 
Reply With Quote
 
 
 
 
Miha Markic [MVP C#]
Guest
Posts: n/a
 
      28th Jan 2005
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

<(E-Mail Removed)> wrote in message
news:E9972E39-7E45-4751-A50F-(E-Mail Removed)...
>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
>
>
>
>



 
Reply With Quote
 
W.G. Ryan eMVP
Guest
Posts: n/a
 
      28th Jan 2005
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
<(E-Mail Removed)> wrote in message
news:E9972E39-7E45-4751-A50F-(E-Mail Removed)...
> 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
>
>
>
>



 
Reply With Quote
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      29th Jan 2005
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."

 
Reply With Quote
 
=?Utf-8?B?UGF1bGIxdXNAbmV3c2dyb3VwLm5vc3BhbQ==?=
Guest
Posts: n/a
 
      30th Jan 2005
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.

"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."
>
>

 
Reply With Quote
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      1st Feb 2005
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."

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parameter date format Stephanie Microsoft Access Queries 7 6th Jun 2009 03:51 AM
converting general date format data into short date format savigliano Microsoft Access Form Coding 3 27th Nov 2006 04:37 AM
Date Format problem in parameter query aaronk23@verizon.net Microsoft Access Queries 2 18th Nov 2005 05:47 PM
Concert date in text to date format for between parameter =?Utf-8?B?bmVpbC5tY2xlYW5AY29ud3kuZ292LnVr?= Microsoft Access External Data 0 24th Nov 2004 04:47 PM
Query parameter date format =?Utf-8?B?VGVycmkgRC4=?= Microsoft Excel Crashes 1 19th Nov 2004 10:32 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:16 PM.