daOnlyBG
daOnlyBG

Reputation: 601

How to declare a SQL Variable that updates with the date?

I'm writing a stored proc and would like to generate a string name that updates with the date. For example, since today is July 21st, 2017, I'd like the string variable to be Today_is_20170721.

Here is what I've tried so far, based on an answer to a question similar to mine:

SET @datestring = 'INSERT INTO @tmpTbl1 SELECT CONVERT(VARCHAR(8), GETDATE(), 112)'
EXEC (@datestring)

@desiredstring = 'Today_is_' + @datestring

I kept getting the error Must declare the scalar variable variable "@datestring".

From where, I'm not sure where to go- I'm fairly certain my variable is declared.

Upvotes: 0

Views: 98

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271023

It occurs to me that you might want:

declare @desiredstring varchar(255);

set @desiredstring = 'Today_is_' + CONVERT(VARCHAR(8), GETDATE(), 112);

However, this is pretty simple SQL code so it might not be what you really want.

Upvotes: 3

Related Questions