Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nosean's avatar

SQL Server Date Time Problem

Hey Guy's

i have trouble with an sql server select query. My Error message:

When converting an nvarchar data type to a datetime data type, the value is out of range. (SQL: select top 1 * from [KHKVKBelege] where [Belegart] = Barverkauf and [Belegdatum] = 2016-09-16 00:00:00.000)

My Query:

        $op = DB::connection('sqlsrv1')
            ->table('KHKVKBelege')
            ->where('Belegart', '=', 'Barverkauf')
            ->where('Belegdatum', '=', '2016-09-16 00:00:00.000')->first();

The value in the column (Belegdatum) is identical.

how do i format the value?

Anyone en Idea?

0 likes
2 replies
laracoft's avatar

What type of SQL server and the datatype of Belegart, Barverkauf and Belegdatum?

I would try changing the date to 2016-09-16 00:00:00 and try again.

Nosean's avatar

Hey Guy's

I solved the problem. As follows:

        // Datum formatieren
        $date = new \DateTime('2020-09-16 00:00:00.000');

        //Belege des ausgewählten Datums
        $receipts = DB::connection('sqlsrv1')
            ->table('KHKVKBelege')
            ->where('Belegart', '=', 'Barverkauf')
            ->whereDate('Belegdatum', '=', $date->format('Y-m-d H:i:s.v'))
            ->get();

please pay attention to ->wheredate

Thank's

Please or to participate in this conversation.