Laravel migration with SQL Server - Stack Overflow

admin2025-04-17  1

SQLSTATE[22007]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Converting a varchar data type to a datetime data type created an out-of-bounds value. (Connection: sqlsrv, SQL: insert into [users] ([name], [email], [password], [updated_at], [created_at]) values (yyyy, [email protected], $2y$12$TOh49tHTJSUzlxE9omOALOiG2FK/pd/MgGVqCSiaoNIfW6dFmX1rW, 2025-01-31 08:15:16.408, 2025-01-31 08:15:16.408))

The migration with $table->timestamps(); created the columns with datetime as type and set this datetime format 'Y-m-d h:i:s.u' when we try an insert but sqlsrv allows 'Y-m-d h:i:s' for datetime type. How can I fix it?

SQLSTATE[22007]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Converting a varchar data type to a datetime data type created an out-of-bounds value. (Connection: sqlsrv, SQL: insert into [users] ([name], [email], [password], [updated_at], [created_at]) values (yyyy, [email protected], $2y$12$TOh49tHTJSUzlxE9omOALOiG2FK/pd/MgGVqCSiaoNIfW6dFmX1rW, 2025-01-31 08:15:16.408, 2025-01-31 08:15:16.408))

The migration with $table->timestamps(); created the columns with datetime as type and set this datetime format 'Y-m-d h:i:s.u' when we try an insert but sqlsrv allows 'Y-m-d h:i:s' for datetime type. How can I fix it?

Share Improve this question edited Jan 31 at 9:51 Thom A 96.3k11 gold badges61 silver badges95 bronze badges asked Jan 31 at 9:38 ManouthéManouthé 92 bronze badges 5
  • All content on Stack Overflow needs to be in English; that includes error messages. – Thom A Commented Jan 31 at 9:39
  • SQL server date time columns don't have a format - they are stored in binary. They are only formatted to display – Dale K Commented Jan 31 at 9:41
  • yyyy-MM-dd hh:mm:ss is an ambiguous string format for (small)datetime. You are better off using yyyyMMdd hh:mm:ss or yyyy-MM-ddThh:mm:ss. If you aren't American then your literal, '2025-01-31 08:15:16.408', would be treated as the 1st day, of the 31st month, of the 2025th year; obviously there aren't 31 months and so it errors. – Thom A Commented Jan 31 at 9:43
  • You should edit getDateFormat so it returns "Y-m-dTh:i:s.u" or whatever to match the format thom wrote is unambigious – siggemannen Commented Jan 31 at 10:36
  • this article on sql server datatypes might be helpfull – GuidoG Commented Jan 31 at 11:51
Add a comment  | 

1 Answer 1

Reset to default 0

Thanks for all of your contributions. For resolving it definitively, I used $table->timestamps(2) instead of $table->timestamps() in the migration file It finally works successfully.

转载请注明原文地址:http://anycun.com/QandA/1744871168a88798.html