make a logdate – SQL Server

@year VARCHAR(4)
,@month VARCHAR(2)
,@day VARCHAR(2)
,@hour VARCHAR(2)

SET @getdate = DATEADD(HH, -1, GETDATE())

SET @year = DATENAME(YY, @getdate)
SET @month = SUBSTRING(CONVERT(VARCHAR(10),@getdate,120),6,2)
SET @day = DATENAME(DD, @getdate)
SET @hour = DATENAME(HH, @getdate)
SET @logdate = @year + CASE WHEN LEN(@month) < 2 THEN ‘0’ + @month ELSE @month END
+ CASE WHEN LEN(@day) < 2 THEN ‘0’ + @day ELSE @day END
+ CASE WHEN LEN(@hour) < 2 THEN ‘0’ + @hour ELSE @hour END

PRINT @logdate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.