Search
Hall of Fame
About Us
All Questions
New Question
Login
SQL Server - write query to fetch reords
Asked By
Dvorkin
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
Dvorkin
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
ODBC Drivers for QuickBooks, Salesforce, SAP, MSCRM, SharePoint … Free Trial!