Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select records less than 1 year old?

This should be a fairly simple question but I know very little about SQL. I have a database which has the following fields:

  • client_id
  • scheduled_amount
  • deposit_amount
  • message_code_id
  • note_text
  • system_date

Now I wish to select all the records that are less than 1 year old from when the SQL statement is run. I know I should use DateDiff, anyone got any ideas?

Thanks

like image 269
Mike Sav Avatar asked Sep 06 '10 14:09

Mike Sav


People also ask

How do I subtract 1 year in SQL?

We can use DATEADD() function like below to Subtract Years from DateTime in Sql Server. DATEADD() functions first parameter value can be year or yyyy or yy, all will return the same result.

How do I SELECT just the year from a date in SQL?

The EXTRACT() function returns a number which represents the year of the date. The EXTRACT() function is a SQL standard function supported by MySQL, Oracle, PostgreSQL, and Firebird. If you use SQL Server, you can use the YEAR() or DATEPART() function to extract the year from a date.

Which statement type would be used to remove transactions more than one year old from a table?

Answer: C. TRUNCATE is a DDL command. It removes the records from the table without any condition. It is not the part of any ongoing transaction and an uncommitted transaction in the session is committed after TRUNCATE is executed.


1 Answers

select * from MyTable where MyDate > DATEADD(year, -1, GetDate()) 
like image 194
D'Arcy Rittich Avatar answered Oct 19 '22 07:10

D'Arcy Rittich