Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to insert DateTime from C# to SQL server DB. I create example query before test in C#. but error occur.

Please anyone help................... What is error in following query???????

SQL
DECLARE @ActivityDate as date
DECLARE @SqlQuery as varchar(1000)

SET @ActivityDate=current_timestamp
SET @SqlQuery='Insert into TestDB (ActivityDate) VALUES ('+CAST(@ActivityDate as datetime)+')'
Exec(@SqlQuery)



Error:
Error during conversion from character string to datetime

Thanks in advance
Posted
Comments
Kornfeld Eliyahu Peter 30-Dec-14 12:29pm    
You know what the error is! Please share with us so we can examine it!

Hi,

this is another solution,
Using sp_executesql is better then using exec, because store the execution plan and avoid syntax error to have clean and elegant code

SQL
DECLARE @ActivityDate as date;
DECLARE @SqlQuery as nvarchar(1000)
DECLARE @ParmDefinition nvarchar(500);

SET @ActivityDate=current_timestamp;
SET @ParmDefinition = N'@ActivityDate datetime';
SET @SqlQuery='Insert into TestDB (ActivityDate) VALUES (@ActivityDate)'

execute sp_executesql @SqlQuery, @ParmDefinition, @ActivityDate = @ActivityDate



hope that help,
Bechir.
 
Share this answer
 
Comments
DoingWork 1-Jan-15 14:43pm    
Yes a better solution. thanks.......
You can't concatenate datetime value with a string.

Problem is in the line
SQL
SET @SqlQuery='Insert into TestDB (ActivityDate) VALUES ('+CAST(@ActivityDate as datetime)+')'


Check following -
SQL
DECLARE @ActivityDate as date
DECLARE @SqlQuery as varchar(1000)
 
SET @ActivityDate=current_timestamp
SET @SqlQuery='Insert into TestDB (ActivityDate) VALUES ('''+CAST(CAST(@ActivityDate as datetime) AS VARCHAR)+''')'
Exec(@SqlQuery)


Note that I have also added 2 single quotations before and after the value after casting it to varchar value.

I hope this was helpful :)

Update 1:
To get complete date with time you need to change the datatype of @ActivityDate to DATETIME as follows
SQL
DECLARE @ActivityDate as DATETIME
DECLARE @SqlQuery as varchar(1000)
 
SET @ActivityDate=current_timestamp
SET @SqlQuery='Insert into TestDB (ActivityDate) VALUES ('''+CAST(@ActivityDate AS VARCHAR)+''')'
Exec(@SqlQuery)


Update 2:
To get value till minute, second & milliseconds you need to use CONVERT instead of CAST. Note that code 113 or 13 will give us the desired result.
SQL
DECLARE @ActivityDate as DATETIME
DECLARE @SqlQuery as varchar(1000)
 
SET @ActivityDate=current_timestamp
SET @SqlQuery='Insert into TestDB (ActivityDate) VALUES ('''+CONVERT(VARCHAR,@ActivityDate,113)+''''
Exec(@SqlQuery)
 
Share this answer
 
v5
Comments
DoingWork 31-Dec-14 14:20pm    
I change data type of @ActivityDate from date to datetime. Then Run this query. Query runs without any error
But seconds are inserted always 00. I want to insert proper seconds too.

RowId.....ActivityDate
11..... 2015-01-01 00:14:00.0000000
12..... 2015-01-01 00:14:00.0000000
13..... 2015-01-01 00:14:00.0000000
14..... 2015-01-01 00:14:00.0000000
15..... 2015-01-01 00:14:00.0000000
Suvendu Shekhar Giri 1-Jan-15 2:15am    
Just change the datatype of @ActivityDate as datetime.

check the updated answer !
DoingWork 1-Jan-15 5:58am    
seconds are not inserted with updated query too.
Suvendu Shekhar Giri 1-Jan-15 6:13am    
Ok. Just using CONVERT instead of CAST should work. Please check the updated solution :)
DoingWork 1-Jan-15 14:43pm    
good....... thanks........

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900