SQL Server - write query to fetch reords - Asked By Priyanka on 12-Dec-12 02:03 AM

i have to write query to retrieve that records from database which have whatever previous date exists from todays date..

 like today is 12/12/2012.  and this query will check for previous date (12/11/2012). if it exists then fetch records... if it does not exist then it will check for (12/10/2012) and so on...

hope you understand my question........
shital patil replied to Priyanka on 10-Jan-13 04:38 AM
Hello Dvorkin,

Please try following script:


Declare @date datetime
Declare @count int
set @date=GETDATE()-1;
set @count=0
        
While (@count<=0)
begin

    select @count = COUNT(*) from tablename where previousdate=@date
    
    if(@count>=1)
    begin
        select * from tablename where previousdate=@date
        break;
    end
    else
        set @date=@date-1;
        
end