Mango
Mango

Reputation: 121

How to INSERT time in SQL Server using data type time

I am writing the query to insert data to the table but I keep getting an error:

Incorrect Syntax near ':'`

This is the query that create the table:

CREATE TABLE Consultation_Slot
(
     Slot_ID CHAR(7) NOT NULL   PRIMARY KEY,
     Appointment_Purpose VARCHAR(255) NULL,
     Appointment_Status VARCHAR(11) NOT NULL,
     Cancellation_Reason VARCHAR(255) NULL,
     Slot_Time time(7) NOT NULL,
     Slot_Date DATE NOT NULL,
     Slot_Day CHAR(10) NOT NULL,
     Room_No VARCHAR(5) NOT NULL,
     Lecturer_ID CHAR(3) NOT NULL
        REFERENCES Lecturer(Lecturer_ID),
     Student_ID CHAR(6) NOT NULL
        REFERENCES Student(Student_ID),
     Schedule_ID CHAR(5) NOT NULL
        REFERENCES Weekly_Consultation_Schedule(Schedule_ID)
)

This is the INSERT statement that I tried to execute:

INSERT INTO Consultation_Slot 
VALUES (1000000,'I need to learn maths','avaliable','', 13:30, 1-28-2018, 
        'Sunday', 'RN001', 1111, 880001, 30001);
GO

Upvotes: 8

Views: 111883

Answers (1)

Racil Hilan
Racil Hilan

Reputation: 25341

You need to surround the DATE and TIME values with apostrophes:

INSERT INTO Consultation_Slot VALUES
       (1000000,'I need to learn maths','avaliable','', '13:30', '1-28-2018',
        'Sunday', 'RN001', 1111, 880001, 30001);

Only number type values don't need apostrophes.

It is also recommended to surround all the other values in your queries with apostrophes. It's true that they are numbers, but the column types are string based, so providing the values as number will cause unnecessary implicit casting:

INSERT INTO Consultation_Slot VALUES
       ('1000000','I need to learn maths','avaliable','', '13:30', '1-28-2018',
        'Sunday', 'RN001', '1111', '880001', '30001');

However, it seems that your table design is incorrect, and some of those columns should be numbers not string, so review your table design.

Upvotes: 11

Related Questions