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.
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.
sql - Calling stored procedure Error: Error converting data type nvarchar to datetime - Stack Overflow
Error converting data type nvarchar to datetime??? – SQLServerCentral Forums
[RESOLVED]SSRS Error converting Datatype NVARCHAR to DATETIME / Too many Arguments Specified
SQL Server : error converting data type nvarchar to datetime - Stack Overflow
Videos
Since you are trying to pass in table name (not in your question but mentioned in the comments) you do need dynamic sql because table names cannot be parameterized. However, you can pass in the name as a parameter to your procedure just like all the values. Then you can parameterize your dynamic sql which will eliminate all the hassle of datatype conversions and such. Something like this should be pretty close. I also added a lot of white space so this is easier to read.
ALTER PROCEDURE [dbo].[myproc]
(
@name varchar(50)
, @ID bigint
, @birthdate datetime
, @Enabled bit
, @married bit
, @employees int
, @hiredDate datetime
, @TableName sysname
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @sql AS NVARCHAR(MAX)
SET @sql = 'IF EXISTS(SELECT * FROM ' + QUOTENAME(@TableName) + ' WHERE ID = @ID )
UPDATE ' + QUOTENAME(@TableName) + '
SET
name = @name
, birthdate = @birthdate
, Enabled = @Enabled
, married = @married
, employees = @employees
, hiredDate = @hiredDate
where ID = @ID
ELSE
INSERT INTO ' + QUOTENAME(@TableName) + '
(
name
, ID
, birthdate
, Enabled
, married
, employees
, hiredDate
)VALUES
(
@name
, @ID
, @birthdate
, @Enabled
, @married
, @employees
, @hiredDate
)'
EXEC sp_executesql @sql,
N'@name varchar(50)
, @ID bigint
, @birthdate datetime
, @Enabled bit
, @married bit
, @employees int
, @hiredDate datetime'
, @name = @name
, @ID = @ID
, @birthdate = @birthdate
, @Enabled = @Enabled
, @married = @married
, @employees = @employees
, @hiredDate = @hiredDate
END
There is no need for dynamic SQL at all:
ALTER PROCEDURE [dbo].[myproc]
(
@name varchar(50),@ID bigint,@birthdate datetime,@Enabled bit
,@married bit ,@employees int,@hiredDate datetime
)
AS
BEGIN
SET NOCOUNT ON
IF EXISTS(SELECT * FROM MyTable WHERE MyTable.ID =@id)
UPDATE MyTable
SET name =name
,birthdate=@birthdate
,Enabled =@Enabled
,married=@married
,employees=@employees
,hiredDate=@hiredDate
where ID = @ID;
ELSE
INSERT INTO MyTable(name,ID,birthdate,Enabled,married,employees,hiredDate)
VALUES(@name, @ID, @birthdate, @Enabled, @married, @employees, @hiredDate) ;
END
EDIT:
I run this sp with:
@birthdate = '2018-12-10 16 45 00',
I propose to provide parameter with culture independent format ISO-8601 YYYY-MM-DDThh:mm:ss
@birthdate = '2018-12-10T16:45:00',
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())