Get records between Today and 2 years from input date
-
I am using Asp.Net 2.0 (c#) and SQL2005. I have a simple table with following info: Column 1: bigint - unique id count field Column 2: varchar - name of organisation Column 3: DataTime - date record added I basically need to show those records that are still valid. By valid i mean those records which have have an input date (column 3) of upto 2 years ago. Once the input DateTime is more than 2 years ago the record should no longer be shown. Please can you give me help / ideas / info on how i can acheive this. Many many thanks in advance!
-
I am using Asp.Net 2.0 (c#) and SQL2005. I have a simple table with following info: Column 1: bigint - unique id count field Column 2: varchar - name of organisation Column 3: DataTime - date record added I basically need to show those records that are still valid. By valid i mean those records which have have an input date (column 3) of upto 2 years ago. Once the input DateTime is more than 2 years ago the record should no longer be shown. Please can you give me help / ideas / info on how i can acheive this. Many many thanks in advance!
In T-SQL this is one of the possibilities:
declare @date as varchar(15) set @date = '2008-04-15' //here u give input parameter select * from myTable where Datetime >= convert(varchar(15),cast(@date as datetime),102) and datetime<= convert(varchar(15),(cast(@date as datetime)-365*2),102)
instead format date 102 use ur according format Hope it helps...
I Love T-SQL
modified on Tuesday, April 15, 2008 7:20 PM
-
In T-SQL this is one of the possibilities:
declare @date as varchar(15) set @date = '2008-04-15' //here u give input parameter select * from myTable where Datetime >= convert(varchar(15),cast(@date as datetime),102) and datetime<= convert(varchar(15),(cast(@date as datetime)-365*2),102)
instead format date 102 use ur according format Hope it helps...
I Love T-SQL
modified on Tuesday, April 15, 2008 7:20 PM
Thanks for the help!!!!! However my SQL knowledge is limited, and i dont know what you mean about '102 date format'. My dates arebeing stored in the format: '15/04/2008 00:00:00' eg Month/Day/Year. Which code do i use for this? Thanks again.
-
Thanks for the help!!!!! However my SQL knowledge is limited, and i dont know what you mean about '102 date format'. My dates arebeing stored in the format: '15/04/2008 00:00:00' eg Month/Day/Year. Which code do i use for this? Thanks again.
-
The
102
datetime format gives u this formatyyyy.MM.dd
Try to run that code on Query Analyzer and see if it is according to your need, if not then fell free to ask ;)
I Love T-SQL
Hi, Ive tried running it and im getting absolutely nothing back. Ive updated the SQL as such for my table/data: declare @date as varchar(15) set @date = '2008-04-16' select * from tbl_QualityStandard where qs_Start >= convert(varchar(15),cast(@date as datetime),102) and qs_Start <= convert(varchar(15),(cast(@date as datetime)-365*2),102)
-
Hi, Ive tried running it and im getting absolutely nothing back. Ive updated the SQL as such for my table/data: declare @date as varchar(15) set @date = '2008-04-16' select * from tbl_QualityStandard where qs_Start >= convert(varchar(15),cast(@date as datetime),102) and qs_Start <= convert(varchar(15),(cast(@date as datetime)-365*2),102)
-
do u have recoreded data in qs_Start column:
qs_Start 2008-04-16 2008-04-15 2008-04-14 2008-04-13
orqs_Start 2008.04.16 2008.04.15 2008.04.14 2008.04.13
or write me how u have inserted data in qs_start column
I Love T-SQL
Hi, My date format is: 15/04/2008 eg day/month/year
-
Hi, My date format is: 15/04/2008 eg day/month/year