How to get date query’s (SQL Server Advanced)

  SQL Advanced

How to get the actual date (YYYYMMDD)

Query:

select ltrim(rtrim(str(year(getdate()))))+right('00'+ltrim(rtrim(str(month(getdate())))),2)+right('00'+ltrim(rtrim(str(day(getdate())-1))),2)

Result:

How to get the actual date subtracting 1 day, 1 month and 1 year. You can use this example also to add. (YYYYMMDD)

Query:

select ltrim(rtrim(str(year(getdate())-1)))+right('00'+ltrim(rtrim(str(month(getdate())-1))),2)++right('00'+ltrim(rtrim(str(day(getdate())-1))),2)

Result: