Home >Database >Mysql Tutorial >How to Correctly Insert Date and Time Values into SQL Server?

How to Correctly Insert Date and Time Values into SQL Server?

Susan Sarandon
Susan SarandonOriginal
2025-01-07 10:16:40448browse

How to Correctly Insert Date and Time Values into SQL Server?

Inserting Date and Time Values in SQL Server

While attempting to insert a datetime value into a SQL Server table, you may encounter errors due to incorrect syntax or data conversion issues. Below, we address these common challenges and provide solutions.

insert into table1(approvaldate)values(18-06-12 10:34:09 AM);

When using this syntax, you would receive an error message indicating incorrect syntax near '10'. This is because SQL Server expects datetime values to be formatted as YYYYMMDD. To resolve this, use the following format:

insert into table1(approvaldate)values('20120618 10:34:09 AM');

If you prefer the dd-mm-yy hh:mm:ss xm format, you will need to use the CONVERT function with the appropriate style:

insert into table1 (approvaldate)
       values (convert(datetime,'18-06-12 10:34:09 PM',5));

In this example, the style '5' represents the Italian culture, where dates are formatted in the dd-mm-yy format. Remember to adjust the style parameter accordingly based on the date culture you are using.

The above is the detailed content of How to Correctly Insert Date and Time Values into SQL Server?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn