Author |
Topic |
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2007-04-18 : 07:11:35
|
How do Iput wildcards around a number in an sp ? If my user leaves BoxNo blank it will list all boxesSELECT *FROM tblFilesWHEREConNo =@strRMUConsignmentNoand FileRef like '%'+@strtxtFileRef+'%'and Subject like '%'+@strtxtSubject+'%'and FileDescription like '%'+@strtxtDescription+'%'and BoxNo like %+@strBoxNo% |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-18 : 07:14:37
|
BoxNo is integer field? or varchar?Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-04-18 : 07:15:06
|
SELECT *FROM tblFilesWHEREConNo =@strRMUConsignmentNoand FileRef like '%'+@strtxtFileRef+'%'and Subject like '%'+@strtxtSubject+'%'and FileDescription like '%'+@strtxtDescription+'%'and (BoxNo = @strBoxNo or boxno is null) |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-18 : 07:15:07
|
and cast(BoxNo as varchar) like '%' + @strBoxNo + '%'Peter LarssonHelsingborg, Sweden |
 |
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2007-04-18 : 08:40:33
|
BoxNo is Integer. I will try the other solutions. Thank you |
 |
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2007-04-18 : 08:54:34
|
I have used PbGuy suggestion as if they enter a 2 they will get boxes 2, 22, 222, 23 etc etc I wanted 2 only. |
 |
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2007-04-18 : 09:11:08
|
Sorry - it does not return any records if they leave BoxNo blank. It works if a BoxNo is entered. Every record will have a BoxNo, but the user may filter on this if they want to, or list all boxes by leaving BoxNo blank. |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-18 : 09:21:57
|
[code]and (BoxNo = @BoxNo or @BoxNo = '')[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2007-04-18 : 10:39:44
|
That does not work because every record will have a BoxNo so if one is not entered to filter on no records satisfy Boxno = '' |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-18 : 10:42:15
|
That is NOT the column criteria. It's the parameter criteria.Have you tried the any of the suggestions made to you?Peter LarssonHelsingborg, Sweden |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-18 : 10:44:37
|
Have you tried my solution at all?It says:and (BoxNo = @BoxNo or @BoxNo = '')and not:and (BoxNo = @BoxNo or BoxNo = '')Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2007-04-18 : 11:10:50
|
Sorry :( I have tried the solutions and a mix of the solutions.Here it is time to go home - I will try again tomorrow. |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-04-18 : 14:32:40
|
I think BPGuy had the solution. Here is some code to try out:DECLARE @Table TABLE (BoxNo INT)DECLARE @BoxNo VARCHAR(10)SET @BoxNo = '2'--SET @BoxNo = ''INSERT @TableSELECT 2 UNION ALLSELECT 22 UNION ALLSELECT 23 UNION ALLSELECT 40 UNION ALLSELECT 32 SELECT *FROM @TableWHERE BoxNo = CAST(@BoxNo AS INT) OR @BoxNo = '' -- OR @BoxNo IS NULL Depending on what @BoxNo is when the user does not specify anything blank or NULL, you may need to adjust the WHERE clause.-Ryan |
 |
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2007-04-19 : 04:26:33
|
Here is my sp now which nearly works !If I enter a Consignment number only - it worksif I enter a Consignment number and Box number and suffix - it worksIf I enter a Consignment number and a Box number and no suffix - it returns boxes with that box number, with a suffix and without a suffix.If there is Box 1A and Box 1 I only want the 1A boxes displayed if a suffix is entered.CREATE PROCEDURE spRMU_GettblFilesandBox@strRMUConsignmentNo nvarchar(10),@strViewRestricted bit,@strtxtFileRef nvarchar (100),@strtxtSubject nvarchar (100),@strtxtDescription nvarchar (100),@strBoxNo Integer,@strBoxSuffix nvarchar (5) ASIF @strViewRestricted = 1 BEGINSELECT *FROM tblFilesWHEREConNo =@strRMUConsignmentNoand FileRef like '%'+@strtxtFileRef+'%'and Subject like '%'+@strtxtSubject+'%'and FileDescription like '%'+@strtxtDescription+'%'and (BoxNo = @strBoxNo or @strBoxNo = '')and (BoxSuffix=@strBoxSuffix or @strBoxSuffix ='')ORDER BY IdENDIF @strViewRestricted = 0 BEGINSELECT *FROM tblFilesWHEREConNo =@strRMUConsignmentNo andRestricted = 0and FileRef like '%'+@strtxtFileRef+'%'and Subject like '%'+@strtxtSubject+'%'and FileDescription like '%'+@strtxtDescription+'%'and (BoxNo = @strBoxNo or @strBoxNo = '')and (BoxSuffix=@strBoxSuffix or @strBoxSuffix ='')ORDER BY IdENDGO |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-19 : 04:30:25
|
SELECT *FROM tblFilesWHEREConNo =@strRMUConsignmentNo andRestricted <= @strViewRestricted -- no need for dual selectand FileRef like '%'+@strtxtFileRef+'%'and Subject like '%'+@strtxtSubject+'%'and FileDescription like '%'+@strtxtDescription+'%'and (BoxNo = @strBoxNo or @strBoxNo = '')and (BoxSuffix=@strBoxSuffix or @strBoxSuffix ='')Peter LarssonHelsingborg, Sweden |
 |
|
|