Depending on your regional settings, the parameter you are passing in for @TimeItemAdded might not be recognized.
You should pass the date in as:
20120720
Answer from LittleBobbyTables - Au Revoir on Stack OverflowDepending on your regional settings, the parameter you are passing in for @TimeItemAdded might not be recognized.
You should pass the date in as:
20120720
Use an unambiguous string literal for your date. In this case:
EXEC dbo.InsertItemDetails
...
, @TimeItemAdded = '20120720';
Better yet, make sure to pass a strongly typed parameter where you know that the date is correct. Ideally, this should never be presented as a string in any format.
Regional formats like m/d/y are bad news, because you can't ensure that they will work given the user's session, dateformat, language settings, the regional settings on the machine, etc.
Got an answer from elsewhere that I will use:
SELECT top 1 multi FROM OPENQUERY([production], 'DECLARE @dt datetime SELECT @dt = GETDATE() exec proddb.dbo.spCalcProjection @dt')
This avoids having to create any additional objects in the db.
Conrad, posting original syntax error for GETDATE() without double quotes could help more than you think. I also don't see why would you need to escape the function here. (Sorry, can't add to your thread with Lamak, not enough reputation for comments). Also, why do you need an open query to call your sp? When you say SQL Server 2008 R2, is it both on the calling side and on your [production] server? If the other end is not SQL Server it might not have GETDATE() function. If the other end is SQL Server you don't need OpenQuery.
[UPDATE] I think I have your answer. You cannot use a function as a parameter for stored procedure. Has nothing to do with open query. What you can do, you can replace that stored procedure with table-valued function. I just tried it and it worked.
Definition:
CREATE FUNCTION TestFun
(
@TestDateParam datetime
)
RETURNS
@RetTable TABLE
(
Line nvarchar(20)
)
AS
BEGIN
INSERT INTO @RetTable
SELECT aString
FROM sometable
WHERE aDate = @TestDateParam
RETURN
END
Call:
SELECT *
FROM dbname.dbo.TestFun(GETDATE())
sql - Calling stored procedure Error: Error converting data type nvarchar to datetime - Stack Overflow
sql server - "Error converting data type varchar to datetime" - Stack Overflow
Error converting data type nvarchar to datetime??? – SQLServerCentral Forums
c# - Error converting data type nvarchar to datetime SQL Server - Stack Overflow
When using OleDb one should always remember that the parameters are not passed according to their names but in the exact order in which they are added to the Parameters collection.
In your code you add first the @repRIS and this is the first parameter passed to the SP. But the SP expects a date for the first parameter and you get the exception
You need to change the order of the insertion in the Parameters collection or switch the declaration order of the parameters in the SP
command.Parameters.Add("@invDt", OleDbType.Date).Value = invDate;
if (repRIS.Length > 0)
command.Parameters.AddWithValue("@repRIS", repRIS);
else
command.Parameters.AddWithValue("@repRIS", DBNull.Value);
And another thing to do is to look at this article Can we stop using AddWithValue already?
I would say that Min Date does not work in SQL from C# since min C# Datetime and the one supported by SQL are different.
Use :
System.Data.SqlTypes.SqlDateTime.MinValue.Value
instead the Min offered by C# Datetime. This should work
'28/1/2013' is an ambiguous format - SQL Server might interpret it as dd/mm/yyyy or as mm/dd/yyyy. In this case, it's done the latter, and doesn't know what the 28th month of the year is.
Use '20130128' instead. This is always interpreted as yyyymmdd.
Had the same error. In my case it was passing datetime2 value to a datetime column:
Had to change string to datetime (3 digits) instead of 7 digits for milliseconds:
exec [dbo].[UpdateProc] @LastViewDate='2016-07-27 11:03:04.1378957'
To:
exec [dbo].[UpdateProc] @LastViewDate='2016-07-27 11:03:04.137'
Try CONVERT(DATETIME, @PaymentDate, 103)
104 is the German style which uses periods between the numerals, as opposed to slashes. 103 is the British/French style.
See: https://msdn.microsoft.com/en-us/library/ms187928.aspx
I've noticed your question is also tagged with c#.
If you are passing the date from c# to sql server,
Don't pass dates as strings. Pass them as DateTime.
The .Net DateTime maps directly to SQL Server's DateTime.
This way, you will not have to deal with the display format at all, since both c# and SQL Server does not store display format in DateTime.
If you really need to convert the string '26/01/2017' to date, you should use 103 for your style argument, as already suggested in other answer.