Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 filtering results with CHARINDEX

Author  Topic 

rternier
Starting Member

44 Posts

Posted - 2009-03-06 : 18:19:15
I'm creating a stored proc for selecting users. I'm trying to get it to work where if a user wants to see everyone who's last name starts with the character "A", it just selects those records, then applies the correct paging to it.

here's what I have... it works when I put in a p_vchLetterFilter, but if that's blank it will not work.

Any thoughts?:


ALTER PROCEDURE [dbo].[procSelectUsers] (
@p_vchLetterFilter VARCHAR(1),@p_vchOrderBy VARCHAR(20), @p_vchQuickSearch VARCHAR(50), @p_intPage int, @p_intPageSize int
)

AS
BEGIN

DECLARE @startRowIndex INT
SET @startRowIndex = (@p_intPage * @p_intPageSize) + 1;


WITH Users as (
SELECT ROW_NUMBER() OVER (ORDER BY intUserID ASC) as Row,
intUserID, vchFirstName, vchLastName,vchUserName, vchPassword,
vchEmail, IsNull(U.intSecurityRoleID,0) as intSecurityRoleID,
IsNull(U.intWorkgroupID,0) as intWorkgroupID, vchInitials, vchBadgeNumber,
SR.vchRoleTitle AS vchSecurityRole, W.vchDescription AS vchWorkgroupDesc
FROM tblUsers U, tblSecurityRoles SR, tblWorkgroups W
WHERE U.sysDateDeleted = 0 AND SR.intSecurityRoleID = U.intSecurityRoleID
AND W.intWorkgroupID = U.intWorkgroupID
AND CHARINDEX(@p_vchLetterFilter, vchLastName, 0) = 1)



SELECT
intUserID, vchFirstName, vchLastName,vchUserName, vchPassword,
vchEmail, intSecurityRoleID, intWorkgroupID, vchInitials, vchBadgeNumber,
vchSecurityRole, vchWorkgroupDesc
FROM Users
WHERE Row BETWEEN @startRowIndex AND @startRowIndex + @p_intPageSize - 1
ORDER BY
CASE
WHEN @p_vchOrderBy = 'last' THEN (RANK() OVER (ORDER BY vchLastName, vchFirstName))
WHEN @p_vchOrderBy = 'first' THEN (RANK() OVER (ORDER BY vchFirstName, vchLastName))
WHEN @p_vchOrderBy = 'username' THEN (RANK() OVER (ORDER BY vchUserName))
WHEN @p_vchOrderBy = 'security' THEN (RANK() OVER (ORDER BY vchSecurityRole, vchLastName, vchFirstName))
WHEN @p_vchOrderBy = 'workgroup' THEN (RANK() OVER (ORDER BY vchWorkgroupDesc, vchLastName, vchFirstName))
END

END /* CREATE PROCEDURE procSelectAllUsers */




----
Killer ASP.NET ninja coding monkeys do exist!
[url]http://weblogs.asp.net/rternier[/url]

rternier
Starting Member

44 Posts

Posted - 2009-03-06 : 18:28:21
I changed my charindex line to:


AND vchLastName LIKE @p_vchLetterFilter + '%'

And it works now :)

----
Killer ASP.NET ninja coding monkeys do exist!
[url]http://weblogs.asp.net/rternier[/url]
Go to Top of Page
   

- Advertisement -