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 :


Google+

Post Comment This is for short comments only. Use the forum for more discussions.




Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer