-1

I have a stored procedure that takes two parameters.

One is a int and another is DateTime that has to be formatted like this: YYYY-MM-dd HH:MM:SS, but the parsing in C# gives me this format: dd-MM-YYYY HH:MM:SS. This is the European format.

How can I parse it to be starting with year, like shown above.

I need it to be a DateTime and not a string, the stored procedure won't allow just string.

Praise
  • 557
  • 2
  • 8
  • 23
  • 4
    A `DateTime` doesn not have _any_ format. What you see it as a _textual_ representation of it. Use _that_ value exactly on your process, not it's string representation or something. Also would be better you see your code and stored procedure definition as well.. – Soner Gönül Jul 08 '15 at 10:15
  • possible duplicate of [C# DateTime to "YYYYMMDDHHMMSS" format](http://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format) – Alex H Jul 08 '15 at 10:29
  • You mention "SQL stored procedure" in your question's title. That is never mentioned again in your question. You've also tagged this question C#. Could you please clarify just what exactly you're trying to do? – stakx - no longer contributing Jul 08 '15 at 11:12
  • I have a store procedure that I want to call. It take two parameters. One is int and another is DateTime. The problem I am having with answers here is as soon as the date is converted toString, it is no longer a DateTime and the store procedure won't accept it as a parameter. Converting back to DateTime will mess up the formatting again. – Praise Jul 09 '15 at 07:00
  • @Praise - With so few details we cannot answer your question. But just as a tip, it looks like you will have to change the SP, not the application. A datetime is a datetime, not a formatted string – Sébastien Sevrin Jul 09 '15 at 07:12
  • @SébastienSevrin I just updated with more info. Hope it explain the question and my problem better. :) – Praise Jul 09 '15 at 08:07
  • @Praise - "if I had this `@closoutDate datetime` changed to this `@closoutDate string` would that solve the invalid argument issue" => no, you would have an invalid data type error. But you get the point, you have to update the SP to either take a `char` or `varchar` parameter (ex: `@closoutDate char(19)`) or leave a `datetime` and cast it in the right format. Again, we can't guess what the SP is doing, you would have to post the part where `@closoutDate` is used. – Sébastien Sevrin Jul 09 '15 at 08:15
  • @SébastienSevrin I updated with the entire SP. – Praise Jul 09 '15 at 08:31
  • @Praise - What is the data type of the column `TidspunktEndret`? – Sébastien Sevrin Jul 09 '15 at 08:44
  • @SébastienSevrin It's DateTime. – Praise Jul 09 '15 at 08:50
  • Then I don't see any issue, I think you may be confused by the storage of the `datetime` type in the database, if you do a `select convert(char(19), [TidspunktEndret], 120) from [Workbookversjon]`, you will have your expected format. If you don't use the `convert`, then it will depend on the client application display behavior. – Sébastien Sevrin Jul 09 '15 at 08:57
  • @SébastienSevrin Just add that to the end of the SP? – Praise Jul 09 '15 at 09:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82794/discussion-between-sebastien-sevrin-and-praise). – Sébastien Sevrin Jul 09 '15 at 09:06

2 Answers2

0

Try it with the following example

DateTime dt = DateTime.Now;
string formattedDate = dt.ToString("yyyy-MM-dd hh:mm:ss");
NCC-2909-M
  • 709
  • 1
  • 7
  • 15
  • The problem I am having here is as soon as the date is converted toString, it is no longer a DateTime and the stored procedure won't accept it as a parameter. Converting back to DateTime will mess up the formatting again. – Praise Jul 09 '15 at 07:01
0

Instead of sending formatted date, Send stored procedure parameter as sql datetime and compare in stored procedure in same format by converting LHS and RHS part of datetime in same format.

Refer this

Amit
  • 882
  • 6
  • 13