How to insert smalldatetime

3.3k Views Asked by At

I'm new to sql server since this is the requirement of my client. I can't insert because of the error I'm getting regarding the smalldatatime. What's wrong with my query? I have tried everything but still it gives me error.

Here are the things I tried:

SET IDENTITY_INSERT table1 ON
INSERT INTO table1
(id, timelog) values
(1, 2018-07-24 06:30:50.000)
SET IDENTITY_INSERT table1 OFF

ERROR I GOT:

Msg 102, Level 15, State 1, Line 5 Incorrect syntax near '06'.

SET IDENTITY_INSERT table1 ON
INSERT INTO table1
(id, timelog) values
(1, '2018-07-24 06:30:50.000')
SET IDENTITY_INSERT table1 OFF

ERROR I GOT:

Msg 206, Level 16, State 2, Line 2 Operand type clash: int is incompatible with date

SET IDENTITY_INSERT table1 ON
INSERT INTO table1
(id, timelog) values
(1, 2018-07-24T06:30:50.000)
SET IDENTITY_INSERT table1 OFF

ERROR I GOT:

Msg 102, Level 15, State 1, Line 5 Incorrect syntax near 'T06:'

1

There are 1 best solutions below

2
Andrea On

The correct syntax is the 2nd, but you are missing the VALUES keyword:

CREATE TABLE table1( id int IDENTITY(1,1) PRIMARY KEY, timelog smalldatetime NOT NULL ); 

SET IDENTITY_INSERT table1 ON
INSERT INTO table1
(id, timelog) values
(1, '2018-07-24 06:30:50.000')
SET IDENTITY_INSERT table1 OFF

select * from table1

Result:

enter image description here