SQL Statement checking for restricted username

Posted on 2005-04-14
Last Modified: 2006-11-18
I've a problem here which is, IF let say when somebody signup with the username of 'adminForever'

How do i restrict him from using the "ADMIN" in his username or others name that listed in my table "tblrestrictedname"???
How do i compare it?

declare @username as nvarchar(50)
set username='adminForever'

if not exists(select * from tblmember where username like @username) ???
Question by:dynamicrevolutions
    LVL 28

    Accepted Solution

    Try this one:

    declare @username as nvarchar(50)
    set @username='adminForever'

    IF EXISTS (SELECT * FROM tblRestrictedName
    WHERE UserName LIKE '%' + @username + '%' OR
               @UserName LIKE '%' + UserName + '%')
    --- Not Allowed
    LVL 23

    Expert Comment

    declare @username as nvarchar(50)
    set @username='adminForever'

    IF NOT EXISTS(SELECT 1 FROM tblRestrictedName WHERE @username LIKE '%' + Name + '%')
            -- Allow username to be used

    This will check whether any restricted names appear *anywhere* in the username.
    So, if "admin" was listed in tblRestrictedName, it would rule out "adminForever", "Foreveradmin", "Hadmint" etc etc

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduced in Microsoft SQL Server 2005, the Copy Database Wizard ( is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    Via a live example, show how to shrink a transaction log file down to a reasonable size.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now