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 Overflow
Discussions

sql - Calling stored procedure Error: Error converting data type nvarchar to datetime - Stack Overflow
Msg 8114, Level 16, State 5, Procedure PP_Sp_ObservationSchedule, Line 0 Error converting data type nvarchar to datetime. More on stackoverflow.com
🌐 stackoverflow.com
Error converting data type nvarchar to datetime??? – SQLServerCentral Forums
Error converting data type nvarchar to datetime??? Forum – Learn more on SQLServerCentral More on sqlservercentral.com
🌐 sqlservercentral.com
November 15, 2007
[RESOLVED]SSRS Error converting Datatype NVARCHAR to DATETIME / Too many Arguments Specified
Hey all, I'm trying to modify this existing SSRS report that our users run on a need-basis ( Link is posted on our SharePoint, they run it whenever they need to pull some order data). The report is built using a couple SPs and an Agent job. The first SP, let's call it Order_SP, builds the table ... More on forums.sqlteam.com
🌐 forums.sqlteam.com
0
0
January 20, 2016
SQL Server : error converting data type nvarchar to datetime - Stack Overflow
EDIT Thank you for all your help. First thing, I have read tons of post and question of people having this same error, mines varies for int and bigint but the error stays the same, I can not inser... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Microsoft Learn
learn.microsoft.com › en-us › archive › msdn-technet-forums › c3391f94-2928-4de2-a61e-0383f8774093
Error converting data type nvarchar to datetime | Microsoft Learn
For these last three types, the dateformat ydm is not supported and therefore, the string '2012-02-29' will always be parsed as YYYY-MM-DD. ... set dateformat dmy; select cast ('01-02-2012' as datetime), cast ('2012-02-01' as datetime), cast ('2012-02-01' as datetime2), cast ('2012-02-01' as date); set dateformat mdy; select cast ('02-01-2012' as datetime), cast ('2012-02-01' as datetime), cast ('2012-02-01' as datetime2), cast ('2012-02-01' as date);
🌐
Edureka Community
edureka.co › home › community › categories › database › error converting data type nvarchar to datetime...
Error converting data type nvarchar to datetime SQL Server | Edureka Community
August 15, 2022 - I encountered this error: Error converting data type nvarchar to datetime When inputting a date in ... 104) Can someone please help me with this?
Find elsewhere
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › error-converting-data-type-nvarchar-to-datetime
Error converting data type nvarchar to datetime??? – SQLServerCentral Forums
November 15, 2007 - To convert mm/dd/yy back to date the dateformat, the current session should be in mdy format. Try to set the dateformat at the beginning of SP as ... The followinge example shows, if you have dmy set as date format, date conversion fails. ... The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
🌐
SQLTeam
forums.sqlteam.com › ssas and ssrs
[RESOLVED]SSRS Error converting Datatype NVARCHAR to DATETIME / Too many Arguments Specified - SSAS and SSRS - SQLTeam.com Forums
January 20, 2016 - Hey all, I'm trying to modify this existing SSRS report that our users run on a need-basis ( Link is posted on our SharePoint, they run it whenever they need to pull some order data). The report is built using a couple SPs and an Agent job. The first SP, let's call it Order_SP, builds the table ...
Top answer
1 of 3
4

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
2 of 3
3

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',
Top answer
1 of 1
1

103 is an ambiguous style. If you must continue with this string-heavy style, at least use unambiguous styles in your conversions.

The unambiguous styles are 112 (for just a date) and 126 (for a date and time). So:

SET @SQL ='SELECT * FROM OPENROWSET(''SQLNCLI'',
''Server=ADMIN-PC;Trusted_Connection=yes;'',
''SET FMTONLY OFF; EXEC [cheminova].[dbo].[SP_Firstdistil] "'+
    convert(varchar(20),@sdate,112)+'" '')'

Although I'd also recommend switching to using sp_executesql which allows you to pass parameters using there natural types rather than converting everything into strings.


Other points from comments, not directly relating to the problem/solution:

1) Using this many temporary tables is usually a sign that you're doing it wrongTM - it feels like you've broken the task down into lots of little procedural steps. Instead, in SQL, what you should try to do is describe the entire task you want to do (rather than how to do it) and let the query optimizer attempt to find the best way to achieve that result.

2) Avoid using the sp_ prefix when naming procedures

3) Stop doing everything by string manipulation. For instance, this:

set @EndDate=CONVERT(varchar,DATEPART(month,@SDate))+'/'+CONVERT(varchar,DATEPART(DAY,@SDate))+'/'+CONVERT(varchar,DATEPART(YEAR,@SDate))+' 06:00:00'

which appears to be attempting to create "today at 6am" could be replaced by:

SET @EndDate = DATEADD(day,DATEDIFF(day,'20000101',@SDate),'2001-01-01T06:00:00')

Which just performs two simple pieces of date arithmetic rather than constructing an (again) ambiguous date format and hoping that SQL Server converts it back into a datetime correctly.

4) And similarly, once that date has been constructed, you can simply DATEADD(day,-1,@EndDate) to produce "yesterday at 6am" rather than doing either of those conversions again.

🌐
GitHub
github.com › rails-sqlserver › activerecord-sqlserver-adapter › issues › 457
Error converting data type nvarchar to datetime2 · Issue #457 · rails-sqlserver/activerecord-sqlserver-adapter
March 17, 2016 - Both fields have the data type datetime2. We are also connecting to an Azure SQL database. Every once in a while we see the following error in our system when the updated_at timestamp is updated: ActiveRecord::StatementInvalid: TinyTds::Error: Error converting data type nvarchar to datetime2.: EXEC sp_executesql N'UPDATE [bookings] SET [updated_at] = @0 WHERE [bookings].[id] = @1; SELECT @@ROWCOUNT AS AffectedRows', N'@0 datetime2(7), @1 int', @0 = N'06-21-2008 04:30:00.5e-05', @1 = 1
Author   dschneider
🌐
Microsoft Power BI Community
community.powerbi.com › t5 › Power-Query › Error-to-convert-nvarchar-to-Datetime › td-p › 792069
Solved: Error to convert nvarchar to Datetime - Microsoft Power BI Community
September 17, 2019 - This is (probably) when power bi tries to convert nvarchar data to date because the date is (probably) in PBI text format. ... Fonte = Sql.Database("serverx", "xx"), dbo_vw_PS = Fonte{[Schema="dbo",Item="dbo_vw_PS"]}[Data], #"Linhas Filtradas" = Table.SelectRows(ddbo_vw_PS, each [DataMovimentacao] > #datetime(2019, 6, 1, 0, 0, 0)), Solved! Go to Solution. ... Maybe we need do some transformation for the date column. ... https://community.powerbi.com/t5/Desktop/Microsoft-Sql-converting-data-type-varchar-10-to-int-only-i...
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › converting-from-nvarchar-to-datetime
Converting from NVarchar to DateTime – SQLServerCentral Forums
September 3, 2009 - http://www.sqlservercentral.com/Forums/Topic18198-8-2.aspx?Highlight=nvarchar+datetime · I also read another but it looked a bit complicated. I found all the records that didn't have real dates by executing this code: Select * from studentcur where isdate(dateofbirth) = 0 ... Conversion failed when converting datetime from character string.
🌐
Stack Overflow
stackoverflow.com › questions › 48437156 › error-converting-data-type-nvarchar-to-datetime
c# - 'Error converting data type nvarchar to datetime' - Stack Overflow
CREATE PROCEDURE [dbo].[Some_Proc] (... @dtAnsweredOn datetime, ... ) AS BEGIN IF(Some_Condition) BEGIN INSERT INTO Some_Table (... dtCreatedOn...) VALUES (...@dtAnsweredOn...) END ELSE BEGIN UPDATE Some_Table SET Some_column = Some_value WHERE dtCreatedOn = @dtAnsweredOn END END ... With Profiler I can see the value passed to the procedure is considered as nvarchar in t-sql for some unknown reason @dtAnsweredOn=N'25.01.2018 16:08:57' ... how about explicitly converting the value?
🌐
Medium
medium.com › @python-javascript-php-html-css › how-to-fix-common-errors-when-converting-nvarchar-to-datetime-in-sql-0061ad539a94
How to Fix Common Errors When Converting NVARCHAR to DATETIME in SQL
December 24, 2024 - Such was the case when I encountered a database storing date-time data in the format ‘02/10/2015 14:26:48’. As I attempted to convert this NVARCHAR value to a DATETIME type for sorting, I used SQL’s CONVERT function. However, instead of achieving my goal, I ran into an error: SQL Error [241]: Conversion failed when converting date and/or time from character string.
🌐
SQL Team
sqlteam.com › forums › topic.asp
Error converting data type nvarchar to datetime. - SQL Server Forums
October 7, 2007 - Microsoft SQL Server articles, forums and blogs for database administrators (DBA) and developers.
🌐
Yearli Guide
help.yearli.com › hc › en-us › articles › 360053938193-Error-Converting-Data-Type-Nvarchar-to-Datetime
Error Converting Data Type Nvarchar to Datetime – Yearli Guide
August 24, 2023 - The Windows Date Format is set incorrectly. Verify/Change Regional Settings Access the Windows Run window by pressing the Windows key and R key on the keyboard. Type "intl.cpl" without the quotes ...