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.
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.
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.
MySQL SYSDATE() Function The SYSDATE() function returns the current date and time.
You don't need the SELECT
DECLARE @LastChangeDate as date
SET @LastChangeDate = GetDate()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With