Reputation: 373
I was trying to execute the following query:
DECLARE @MyValue DATETIME
SET @MyValue = CAST((SELECT CURRENT_TIMESTAMP) as DATETIME)
INSERT INTO Person (Gender,Name, JoinDate)
VALUES ('M','Alfred',@MyValue)
but SQL Server keeps throwing the following error:
Msg 241, Level 16, State 1, Procedure Insert_Current_Time, Line 8 Conversion failed when converting date and/or time from character string.
I've already tried many other things, but none of them seemed to solve it. Is there any solution for this?
Upvotes: 1
Views: 6223
Reputation: 164
Is JoinDate a string/varchar datatype? If so you need to cast it to a string instead of trying to store a datetime value in a string field.
Check this out: How to convert DateTime to VarChar
Upvotes: 0
Reputation: 3846
Just do this:
INSERT INTO Person (Gender,Name, JoinDate)
VALUES ('M','Alfred',getdate())
or this:
INSERT INTO Person (Gender,Name, JoinDate)
VALUES ('M','Alfred',current_timestamp)
Upvotes: 1
Reputation: 14471
Can you put the CURRENT_TIMESTAMP into your insert and dispense with the cast? This works for me:
create table flar ( thetime datetime);
insert into flar values( CURRENT_TIMESTAMP);
Upvotes: 0