NealR
NealR

Reputation: 10709

User GETDATE() to put current date into SQL variable

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.

Upvotes: 24

Views: 147851

Answers (5)

Charles Bretana
Charles Bretana

Reputation: 146597

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()

Upvotes: 13

whereisSQL
whereisSQL

Reputation: 638

You can also use CURRENT_TIMESTAMP for this.

According to BOL CURRENT_TIMESTAMP is the ANSI SQL euivalent to GETDATE()

DECLARE @LastChangeDate AS DATE;
SET @LastChangeDate = CURRENT_TIMESTAMP;

Upvotes: 1

Taryn
Taryn

Reputation: 247860

You don't need the SELECT

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

Upvotes: 42

Andreas
Andreas

Reputation: 1791

SELECT @LastChangeDate = GETDATE()

Upvotes: 2

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

Upvotes: 3

Related Questions