question

oussa.zia avatar image
oussa.zia asked

can we use DATEDIIF with DELITE FROM

DELETE FROM `emp` WHERE DATEDIFF(`year`,`2019-01-01`,emb)>15

I NEED TO HAVE THE employes that spend around here more than 15years and get them off this database or at least this table plz help

sqlsql-server
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
KenJ avatar image
KenJ answered

You certainly can. Note the syntax - you need the earlier date before the later date:

DATEDIFF ( datepart , startdate , enddate )

And the year keyword doesn't require quotes. You can find specific examples in the documentation here - https://docs.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql?view=sql-server-2017

You can test it out by changing the delete to a select and confirming that it is identifying the rows you want to remove:

select * from emp
where datediff(year, emb, '2019-01-01') > 15

Once you are sure that it's right, you can change the select * back to a delete

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.