2

Trying to pass date into a stored procedure as follows:

@dateRegistered = '28/04/2012'

But it keeps telling me:

Error converting data type varchar to date.

It works if I do it as:

@dateRegistered = '04/28/2012'

But this is not the format I want to use.

I have run the following query to set the format:

SET DATEFORMAT dmy

Why isn't it working?

Edit: Here is how I did it:

In my stored procedure, I put:

SET DATEFORMAT dmy

Then from code, I pass it as:

myCommand.Parameters.Add("@dateRegistered", SqlDbType.Date);
myCommand.Parameters["@dateRegistered"].Value = DateTime.Now.ToString("dd/mm/yyyy");

I passed it as string to ensure that even if my computer (or the server) has a different date format, it will always use dmy.

Edit Edit:

I passed it as datetime.now. It seems to transform it to the default format when I send it, and then transform it back to my computer format when I read it. Unsure how exactly this happens, but it seems to be working fine.

1
  • 1
    "I passed it as string to ensure that even if my computer (or the server) has a different date format, it will always use dmy.". NO, NO, NO Pass it as a DateTime - the .NET libraries know how to translate the .NET concept of datetime into the SQL concept, and don't need to muck about with any string formatting or ambiguity. Commented Apr 28, 2012 at 11:26

4 Answers 4

3

It looks like the parameter is a date (in the database), while you try to pass a string (which corresponds to varchar in the database). If you instead pass a .NET DateTime object it should work as expected.

Sign up to request clarification or add additional context in comments.

3 Comments

So how do you pass the date in the sql management studio? (For testing)
@TheGateKeeper in your case you could try convert(datetime, '04/28/2012', 101) (assuming you are using SQL Server). I am not a SQL Server master so there may be better ways.
If this answer were correct, it would also fail for the month-first format, but it doesn't
2

Save yourself the headache and use an unambiguous date format - 20120428 (YYYYMMDD) or 2012-04-28.

As an aside: you mention you're using C# (based on the tags) - how are you using ADO.NET? Or is it Linq to SQL / EF / some other ORM?

Edit:

Ok, use the CONVERT function with an appropriate parameter indicating the format of your date string

EG:

declare @D datetime
set @D = CONVERT(datetime, '04/28/2012', 101)
print @D

101 is US standard format mm/dd/yyyy.

Using convert will ensure it ALWAYS works regardless of any environment settings such as the date order.

3 Comments

It seems to work from code when I pass a datatime object... but I want to be able to test it from the SQL Management Studio too. Btw, I don't want to use the (YYYYMMDD) format.
2012-04-28 doesn't work in SQL Server, for a datetime, if you've got british language set. (20120428 does)
Can you please check If I did it correctly? I updated the question.
1

If you call set dateformat dmy, that only affects the current connection. You'd have to set it every time right before you convert the string to a date.

The best solution is probably the ISO format, like @IanYates suggests.

4 Comments

So it isn't something you set for the database one time? I have to set it each time I pass a parameter?
Or better, pass a datetime myCommand.Parameters["@dateRegistered"].Value = DateTime.Now;
Can you please explain how to use the ISO format? If I call DateTime.Now wont this return the date with the format specified to the computer?
ISO format is DateTime.Now.ToString("yyyy-MM-dd"), note that mm means minutes, and MM months, see MSDN
1

dear your code is working after using * SET DATEFORMAT dmy because you are passing date as "dd/MM/yyyy" like "DateTime.Now.ToString("dd/mm/yyyy")"

check the date format in your database by following command :-

  • dbcc useroptions

and check "dateformat : mdy" by default. So passing your date in "MM/dd/yyyy" format will also work fine.

Happy Coding :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.