Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting getdate() to yyyymmdd & getting the date two years back

I have found a couple of different methods to convert it. However, I still get the yyyy-mm-dd format or yyyy-mm-dd hh:mm:ss. I am currently using SQL Server 2014.

SELECT dateadd(day, convert(int, getdate()), 112)
SELECT DATEADD(YEAR, -2, convert(DATE, GETDATE(), 112))

I am doing a date range of 2 years. Thus I need the codes to the find the date two years back.

like image 606
Brandon Avatar asked Jul 25 '16 08:07

Brandon


Video Answer


2 Answers

You can also use FORMAT:

 select FORMAT(getdate(), 'yyyyMMdd')
like image 104
vercelli Avatar answered Oct 19 '22 20:10

vercelli


Try CONVERT(char(8), GETDATE(), 112)

Also check https://technet.microsoft.com/en-us/library/ms187928(v=sql.105).aspx

like image 21
nilsman Avatar answered Oct 19 '22 19:10

nilsman