Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to INSERT time in SQL Server using data type time

Tags:

sql

sql-server

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
like image 205
Mango Avatar asked Jan 28 '18 15:01

Mango


People also ask

How can I add time to date in SQL Server?

To combine date and time column into a timestamp, you can use cast() function with concat(). select cast(concat(yourDateColumnName, ' ', yourTimeColumnName) as datetime) as anyVariableName from yourTableName; In the above concept, you will use cast() when your date and time is in string format.


1 Answers

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.

like image 150
Racil Hilan Avatar answered Oct 06 '22 00:10

Racil Hilan