Example of Date Queries in MSSQL database
We 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 dt
We 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 record
We can get the recods of last 15 days from current day like this.
select * from dt where datediff(dd,issue_dt,GETDATE()) < 15
We can change the above query and get desired result in Month or Years .
Be the first to post comment on this article :