Example of Date Queries in MSSQL databaseWe can collect records based on various functions based on date field. Date Query examples
Getting records of today
select * from dt where issue_dt = GETDATE()In our library issue table we have a issue date, so we can use this to list all books issued today Let us find out number of days passed since the issue date for all the books
select datediff(dd,issue_dt,GETDATE())as diff, issue_dt from dtWe will try to list this in order of highest number of days to lowest number of days by using order by
select datediff(dd,issue_dt,GETDATE())as diff, issue_dt from dt order by diff desc
Last 15 days recordWe can get the recods of last 15 days from current day like this.
select * from dt where datediff(dd,issue_dt,GETDATE()) < 15We can change the above query and get desired result in Month or Years .
This article is written by plus2net.com team.
Be the first to post comment on this article :