Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User GETDATE() to put current date into SQL variable

Tags:

I'm trying to get the current date into a variable inside a SQL stored procedure using the following commands

DECLARE @LastChangeDate as date
SET @LastChangeDate = SELECT GETDATE()

This gives me the following error: "Incorrect Syntax near 'SELECT'"

This is the first stored procedure I've ever written, so I'm unfamiliar with how variables work inside SQL.

like image 626
NealR Avatar asked Jul 30 '12 16:07

NealR


People also ask

How do I assign a current date to a variable in SQL?

Usage Options. SQL Server provides several different functions that return the current date time including: GETDATE(), SYSDATETIME(), and CURRENT_TIMESTAMP. The GETDATE() and CURRENT_TIMESTAMP functions are interchangeable and return a datetime data type. The SYSDATETIME() function returns a datetime2 data type.

How can I insert current date in SQL query?

SQL Server GETDATE() Function The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format.

Which query is used to get the current date in SQL?

MySQL SYSDATE() Function The SYSDATE() function returns the current date and time.


2 Answers

You don't need the SELECT

DECLARE @LastChangeDate as date
SET @LastChangeDate = GetDate()
like image 59
Taryn Avatar answered Sep 23 '22 13:09

Taryn


Just use GetDate() not Select GetDate()

DECLARE @LastChangeDate as date 
SET @LastChangeDate = GETDATE() 

but if it's SQL Server, you can also initialize in same step as declaration...

DECLARE @LastChangeDate date = getDate()
like image 20
Charles Bretana Avatar answered Sep 24 '22 13:09

Charles Bretana