0

I have four tables:

1-dbo.Projects(id,ProjectName,Areas,PaymentSystem,location.id,purpose.id,types.id,etc)

2-dbo.Locations (id, location name)

3-dbo.Purpose (id, Purposename)

4-dbo.Types (id, typname)

I have a search criteria, this criteria is filled with data from database tables: locations, purpose and types, also have textboxes for max and min values.

I will search with all fields and max, min values - I reached to this query but missed the query to get or filter max and min :

ALTER FUNCTION SearchProjects (
@location NVARCHAR(50),
@purpose NVARCHAR(50),
@type NVARCHAR(50),
@max nvarchar(50),
@min nvarchar(50))
RETURNS TABLE
AS
  RETURN
  (
  SELECT  p.ProjectName, 
        p.Areas, 
        p.PaymentSystem, 
        p.ReceivedDate,    
        p.PropertyClassification, 
        p.ProjectImage,         
        l.LocationName,
        Pur.PurposeName,            
        t.TypeName
FROM dbo.Projects AS p 
LEFT JOIN dbo.Locations AS l ON p.LocationID = l.ID      
LEFT JOIN dbo.Purpose pur ON p.PurposeID = pur.ID 
LEFT JOIN dbo.[Types] AS t ON p.TypeID = t.ID
WHERE UPPER(ISNULL(l.LocationName,N'')) LIKE N'%' + UPPER(@location) + '%'
AND UPPER(ISNULL(pur.PurposeName,N'')) LIKE N'%' + UPPER(@purpose) + '%'
AND UPPER(ISNULL(t.TypeName,N'')) LIKE N'%' + UPPER(@type) + '%'
AND p.Areas like @max
AND p.Areas like @min
 )
 GO
SELECT * FROM dbo.SearchProjects('','','','200','250');
4
  • 1
    What are you asking ? Please be specific. Commented Nov 22, 2016 at 9:46
  • I want to pass max and min values to function then return filter on area column with the values , if i have in area column values from 100 to 300 in each records i want pass min value 100 and max value 200 then get all result matched with this values Commented Nov 22, 2016 at 9:54
  • Possible duplicate of Create and execute function in SQL Server Commented Nov 22, 2016 at 9:56
  • @IvanStarostin no duplicate the question is different Commented Nov 22, 2016 at 9:58

1 Answer 1

1

From my understanding you want filter for your area column for max and min value which you can achieve by using BETWEEN

AND p.Areas BETWEEN @min AND @max

So your WHERE condition will look something like,

WHERE 
    UPPER(ISNULL(l.LocationName,N'')) LIKE N'%' + UPPER(@location) + '%'
    AND UPPER(ISNULL(pur.PurposeName,N'')) LIKE N'%' + UPPER(@purpose) + '%'
    AND UPPER(ISNULL(t.TypeName,N'')) LIKE N'%' + UPPER(@type) + '%'
    AND p.Areas BETWEEN @min AND @max
Sign up to request clarification or add additional context in comments.

4 Comments

i need something else when i call this line get all results : SELECT FROM dbo.SearchProjects('','','','','');
if i do not pass min or max value get all results
@hashim That will happen as you are not passing anything to filter out data. If you need anything else in please specify in question with details.
ok, no i tried to select without passing values no result return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.