Declare @StartNo nvarchar(64) Declare @EndNo nvarchar(64) Declare @Special bit set @StartNo = 'I1904010002' set @EndNo = 'I1904010002' set @Special=1 SELECT IndentBill.*, Customer.No_Customer, Customer.Name_Customer, Customer.ShortName_Customer, Customer.Type_Customer, Customer.LastDiscount_Customer, Customer.PostalCode_Customer, a.Name_User AS Name_Creator, ISNULL(b.Name_User, '') AS Name_Assessor, ISNULL(c.Name_User, '') AS LastModUserName, Location.No_Location, Location.Name_Location, Employee.No_Employee AS No_Principal,Employee.Name_Employee AS Principal_IndentBill,Customer.ID_Creator As CustomerCreator FROM IndentBill LEFT JOIN Customer ON Customer.ID_Customer = IndentBill.ID_Customer LEFT JOIN AppUser a ON a.ID_User = IndentBill.ID_Creator LEFT JOIN AppUser b ON b.ID_User = IndentBill.ID_Assessor LEFT JOIN AppUser c ON c.ID_User = IndentBill.LastModUser_IndentBill LEFT JOIN Location ON Location.ID_Location = IndentBill.ID_Location LEFT JOIN Employee ON Employee.ID_Employee = IndentBill.ID_Principal WHERE IndentBill.No_IndentBill >= (case DataLength(@StartNo) when 0 then IndentBill.No_IndentBill else @StartNo end) and IndentBill.No_IndentBill <= (case DataLength(@EndNo) when 0 then IndentBill.No_IndentBill else @EndNo end) and Special_IndentBill=case @Special when 1 then Special_IndentBill else 0 End --added for Special SELECT IndentBillProduct.*, Product.No_Product, Product.Name_Product, Product.Unit_Product, Product.LowSalePrice_Product, BillType.Name_BillType, (IndentBillProduct.Quantity_IndentBillProduct * IndentBillProduct.UnitPrice_IndentBillProduct ) AS Amount_IndentBillProduct ,IndentBill.No_IndentBill FROM IndentBillProduct LEFT JOIN Product ON Product.ID_Product = IndentBillProduct.ID_Product LEFT JOIN BillType ON BillType.Value_BillType = IndentBillProduct.Type_FromBill LEFT JOIN IndentBill ON IndentBillProduct.ID_IndentBill = IndentBill.ID_IndentBill WHERE IndentBill.No_IndentBill >= (case DataLength(@StartNo) when 0 then IndentBill.No_IndentBill else @StartNo end) and IndentBill.No_IndentBill <= (case DataLength(@EndNo) when 0 then IndentBill.No_IndentBill else @EndNo end) and Special_IndentBill=case @Special when 1 then Special_IndentBill else 0 End --added for Special ORDER BY CAST(IndentBillProduct.No_IndentBillProduct AS INT) SELECT ReportComment.*, IndentBill.No_IndentBill FROM ReportComment LEFT JOIN IndentBill ON ReportComment.ID_Bill = IndentBill.ID_IndentBill WHERE IndentBill.No_IndentBill >= (case DataLength(@StartNo) when 0 then IndentBill.No_IndentBill else @StartNo end) and IndentBill.No_IndentBill <= (case DataLength(@EndNo) when 0 then IndentBill.No_IndentBill else @EndNo end) and Special_IndentBill=case @Special when 1 then Special_IndentBill else 0 End --added for Special ORDER BY ReportComment.No_ReportComment