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

  • Home
  • SEARCH
  • 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 7734755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:19:50+00:00 2026-06-01T07:19:50+00:00

I have a very large script which is creating multiple views. In a number

  • 0

I have a very large script which is creating multiple views. In a number of the views there is the same piece of script being used. This script is listed below.

CASE WHEN pc.[user_name] IN
(
    SELECT [user_name]
    FROM people AS p WITH(NOLOCK) 
    WHERE p.status_id = 1 p.last_login > DATEADD(MONTH, -12, GETDATE())
    AND p.[user_name] NOT IN 
    (
        SELECT p.[user_name]
        FROM people AS p WITH(NOLOCK)
        WHERE p.status_id IN (1,2) AND p.[user_name] LIKE '%2'
    )
) THEN pc.[user_name]
ELSE 'standarduser' END AS created_by

Could someone point me in the right direction of how to write a function where I can pass in pc.[user_name] and it will return the correct value? I am new to functions in SQL. Thank you.

  • 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-06-01T07:19:51+00:00Added an answer on June 1, 2026 at 7:19 am

    The function can be created as follows:

    CREATE FUNCTION [dbo].[FunctionName] (@UserName VARCHAR(50)) 
    RETURNS VARCHAR(50) 
    AS
    BEGIN   
        RETURN  COALESCE(
                    (   SELECT  [User_name]
                        FROM    people AS p WITH(NOLOCK)      
                        WHERE   p.status_id = 1 
                        AND     p.last_login > DATEADD(MONTH, -12, GETDATE())     
                        AND     p.[user_name] NOT LIKE '%2'
                        AND     [User_name] = @UserName
                    ), 'StandardUser')
    END
    

    I have tried to simplify your select as much as possible based on the assumption that user_name is unique, i.e. a user_name cannot have both a status_ID of 1 and 2, this allowed me to remove a NOT IN from the statement, as you are first selecting a group of users where the status_ID is 1 then removing from them where the status_ID is 1 or 2 and the user name ends in 2. Since there will be be nobody in the first group who does not have a status_ID of 1, you can then just remove those in the first group whose username ends in 2, which doesn’t require a subquery, just a where clause.

    Having said all that, I would still be inclined to use a view or similar set based solution to achieve the same result. Something like:

    SELECT  *, COALESCE(a.[User_Name], 'StandardUser') [NewUserName]
    FROM    People p
            LEFT JOIN 
            (   SELECT  [User_name]
                FROM    people AS p WITH(NOLOCK)      
                WHERE   p.status_id = 1 
                AND     p.last_login > DATEADD(MONTH, -12, GETDATE())     
                AND     p.[user_name] NOT LIKE '%2'
            ) a
                ON p.[User_name] = a.[User_name]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very large script which contains a lot of php files, so
Hello I have a very large SQL script and while trying to execute it
I have a very large php maintenance script (basically it recreates thumbnails for an
I have a very large (~6GB) SVN repository, for which I've written a batch
I have a very large file that looks like this (see below). I have
I have some very large processing being done in a php application. I'd like
I have a PHP script which reads a large CSV and performs certain actions,
I have a shell script which shuffles a large text file (6 million rows
I have a very large code, a lot of which is legacy code. I
I have a very large image that I would like to show a 200x200px

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.