Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6613981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:17:00+00:00 2026-05-25T20:17:00+00:00

I have been struggling for quite some time to get this query going. In

  • 0

I have been struggling for quite some time to get this query going.

In short my query searches by fileno and/or searchfield

DECLARE @pSearchFor AS NVARCHAR(100);

— I am here testing with null value, ‘ ‘ , or seperate words
SET @pSearchFor = null — ‘”marsa” and “mosta”‘;

 IF ISNULL(@pSearchFor,'') = '' SET @pSearchFor = '""' ;

declare @fileNo nvarchar(50) = 'e/e'

select top 1000 r.FileId, r.FileNo,  fs.SearchField, @pSearchFor

from regfile as r
left outer join FileSearchFields as fs on r.FileId = fs.FileID
where r.FileNo like 
CASE 
 WHEN  Len(@fileno) > 1 THEN '%'+@fileNo+'%'
 ELSE r.FileNo
END

 AND 
     1 = 
     CASE WHEN ISNULL(@pSearchFor, '') = '' THEN 1 ELSE 0 END
       or CONTAINS(fs.SearchField, @pSearchFor)

I am getting nothing returned if @pSearchFor is null otherwise works great.

I need to return all instances if a null

One possible solution might be to call 2 seperate sps or use if /else but probably exists a better method.

I really do appreciate your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:17:01+00:00Added an answer on May 25, 2026 at 8:17 pm

    I have solved the problem. Maybe this may be of some help to others!
    This is a snippet of my stored procedure.

     @fileNo nvarchar(50) = null ,
     @fields nvarchar(100) = '""',`enter code here`
     @datefrom date = null,
     @dateto date  = null,
    ...
    AS`enter code here`
    BEGIN
    
     if (@fields = null or LEN(@fields) < 1 ) set @fields = '""'
    
    select top 1000 r.*,
    (CASE 
            WHEN fs.SearchField IS NULL THEN CONVERT(NVarChar(1),'')
            ELSE CONVERT(NVarChar(MAX),fs.SearchField)
         END) AS [Search]  
    from regfile as r
    left outer join FileSearchFields as fs on r.FileId = fs.FileID
    where r.FileNo like 
    CASE 
     WHEN  Len(@fileno) > 1 THEN '%'+@fileNo+'%'
     ELSE r.FileNo
     END
     and
      r.Date between 
    CASE 
     WHEN  @datefrom != '' THEN @datefrom
     ELSE '1900-1-1'
     END
    
     and 
    CASE 
     WHEN  @dateto != '' THEN @dateto
     ELSE '9999-1-1'
     END
     and 
     ((LEN(@fields) > 2 and contains(fs.SearchField,@fields))or (LEN(@fields) <= 2)) 
     --NB: <= 2 as we have added the "" characters in @fields!
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling with this for quite some time having been accustomed to
I have been struggling with this for quite some time now. jQuery.ajax in the
I have been struggling quite a bit to get this JQgrid to work with
I've been struggling with this for quite some time but i just can't figure
I've been struggling with this for quite some time now, although I am pretty
I am really struggling with this and have been at it for quite some
I've been struggling with this for quite some time now and I couldn't find
I have been struggling with this problem for quite a long time now. I
Alright. This may be difficult but I have been struggling for quite a bit
I have been struggling with getting this query right for hours now. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.