public function up(): void
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->string('book_name');
$table->string('author');
$table->integer('page_count');
$table->foreignId('category_id')->constrained()->onDelete('cascade');
$table->string('isbn');
$table->string('publisher');
$table->string('publish_year');
$table->string('status');
$table->timestamps();
});
}
SQLSTATE[22007]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. (Connection: sqlsrv, SQL: insert into [books] ([book_name], [author], [page_count], [category_id], [isbn], [publisher], [publish_year], [status], [updated_at], [created_at]) values (1984, Gee Orwell, 328, 4, 9780451524935, Penguin Books, 1949, available, 2025-03-17 14:01:33.722, 2025-03-17 14:01:33.722))
I am working on a website but I always get the same error when trying to add data with seeder. I tried using datetime instead of timestamps and although I made it nullable, it continues to give the same error. I use SQL Server Management Studio.
public function up(): void
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->string('book_name');
$table->string('author');
$table->integer('page_count');
$table->foreignId('category_id')->constrained()->onDelete('cascade');
$table->string('isbn');
$table->string('publisher');
$table->string('publish_year');
$table->string('status');
$table->timestamps();
});
}
SQLSTATE[22007]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. (Connection: sqlsrv, SQL: insert into [books] ([book_name], [author], [page_count], [category_id], [isbn], [publisher], [publish_year], [status], [updated_at], [created_at]) values (1984, Gee Orwell, 328, 4, 9780451524935, Penguin Books, 1949, available, 2025-03-17 14:01:33.722, 2025-03-17 14:01:33.722))
I am working on a website but I always get the same error when trying to add data with seeder. I tried using datetime instead of timestamps and although I made it nullable, it continues to give the same error. I use SQL Server Management Studio.
Share Improve this question edited Mar 17 at 14:19 Ahmet Faruk Yaşar asked Mar 17 at 14:16 Ahmet Faruk YaşarAhmet Faruk Yaşar 112 bronze badges 10 | Show 5 more comments1 Answer
Reset to default 0I would consider another solution:
First make the published_year
an integer type, like this:
$table->integer('publish_year');
Then for some i would have tried to add both created_at
and updated_at
manually in the seeder:
'created_at' => now(),
'updated_at' => now(),
datetime
value looks to be in an ambiguous format fordatetime
; I suspect it is being read as ayyyy-dd-MM
value. Considering the value as well (end with.722
) I would suggest thatdatetime
is the wrong data type for several reasons, including it's not accurate enough (datetime
cannot stored the value2025-03-17 14:01:33.722
and would round it to the nearest 1/300th of a second). Have you considered using adatetime2
with an appropriate precision? – Thom A Commented Mar 17 at 14:21ODBC Driver 17 for SQL Server
is very old. Update to 18. The date-related database types have no format either, they're binary values. Instead of trying to convert them to strings specify the timestamp precision, eg$table->timestamps(2);
. That's the real fix in the related Laravel issue – Panagiotis Kanavos Commented Mar 17 at 14:51