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

willa75's avatar

SQLSTATE[22007] SQL Server Date issue

Hey all,

I am having trouble updating datetime fields in the database for Microsoft SQL Server (migrating existing app to Laravel). I get various errors depending on what I set my date formatting to, but I can't seem to stop the errors:

Y-m-d H:i:s.v and Y-m-d H:i:s.u gives me the error:

Conversion failed when converting date and/or time from character string.

**Y-m-d H:i:s.v0** gives me the error:

Data missing

And Y-m-d H:i:s gives me the error:

Trailing Data

I'm not sure what else to use, but this is the structure of dates saved in the db is *2019-07-12 14:34:28.000*. If you have any recommendations I would love to hear them, thanks!

EDIT: I should clarify that I am using Carbon to generate the dates that give me the error like so:

Carbon::now()->addDays(1)->format($this->dateFormat);
0 likes
7 replies
jlrdw's avatar

I have a case where I import a csv, and what I do is:

$tdate = new \DateTime($array[$i][2]); 
    // comes in as 2/15/2019 just example
$ndate = $tdate->format('Y-m-d');

And why do you need the time on a date field.

Cronix's avatar

Y-m-d H:i:s.v should work as sql server, as I understand, only uses millisecond precision (3 digit precision)

You seem to be adding an extra 0 to the end and making it 4 digits with Y-m-d H:i:s.v0?

Cronix's avatar

And why do you need the time on a date field.

OP clearly stated it was a datetime field.

1 like
willa75's avatar

@cronix I added 0 once, because I saw it in an answer when I was searching the internet for a solution, so it was just something I tried. The confusing thing is this was working for me before, and I did fix it with Y-m-d H:i:s.v, but now that gives me the conersaion failed error. And my app is version 5.8

@jlrdw unfortunately that still leaves me with the error

Trailing data

It's like it wants Y-m-d H:i:s.v, but when I give it that I get a conversion error.

jlrdw's avatar

Go to the server version you are using check the documentation and format exactly like they say.

willa75's avatar

So, I went to the site to see the dates coming back very similarly to Y-m-d H:i:s.v, although I noticed mine had extra decimals sometimes, so I replaced the carbon code with:

date('Y-m-d H:i:s.v', strtotime('+1 days'));

And now the issue appears to be resolved, although I'm not sure why when I was literally using the excat same format. Thanks @jlrdw and @cronix for the help!!!

Please or to participate in this conversation.