Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update SQL date using NOW() [duplicate]

Tags:

sql

php

Basically I have a simple login form. In the database I have a 'last_logged' column and would like to update it with the current date and time every time someone logs in.

I currently have this query:

    UPDATE users SET last_logged = "NOW()" WHERE id = 1

But it doesn't update the column to the current date. Any ideas why?

like image 627
CarlTaylor1989 Avatar asked Jun 16 '12 15:06

CarlTaylor1989


2 Answers

Remove the quotes from NOW(). As a function call, it should be unquoted.

UPDATE users SET last_logged = NOW() WHERE id = 1
like image 119
Michael Berkowski Avatar answered Oct 12 '22 12:10

Michael Berkowski


MS SQL uses GETDATE() rather than NOW()

(Just an FYI)
In SQL-Server I now use SYSDATETIME():

DECLARE @now DATETIME = DATEADD(dd,DATEDIFF(dd,'19000101',SYSDATETIME()),'19000101');
like image 37
whytheq Avatar answered Oct 12 '22 14:10

whytheq