Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 how to get current date as integer yyyymmdd

Author  Topic 

theboyholty
Posting Yak Master

226 Posts

Posted - 2011-04-20 : 05:29:53
I've always worked with GETDATE() but now I need to append a datestamp onto files and processed tables. How can I SIMPLY get the current date in the format YYYYMMDD as an integer?

---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk The official unofficial website of Bury Football Club

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-04-20 : 05:33:12
select convert(int,convert(varchar(8),getdate(),112))



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

theboyholty
Posting Yak Master

226 Posts

Posted - 2011-04-20 : 05:39:19
Thank you. I shall add that to my arsenal of SQL weaponry.

---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk The official unofficial website of Bury Football Club
Go to Top of Page

theboyholty
Posting Yak Master

226 Posts

Posted - 2011-04-20 : 05:42:00
In fact I've had a fiddle with it and it could be reduced further to:

SELECT CONVERT(VARCHAR,GETDATE(),112)

---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk The official unofficial website of Bury Football Club
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-04-20 : 05:46:38
1. It is always a good idea to give the length when converting to varchar.
2. Your need was to get an INT.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-04-20 : 10:42:12
This is the fastest way that I know of:
DECLARE @Date DATETIME = CURRENT_TIMESTAMP
SELECT YEAR(@Date) * 10000 + MONTH(@Date) * 100 + DAY(@Date)
Go to Top of Page
   

- Advertisement -