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 6353103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:21:05+00:00 2026-05-24T22:21:05+00:00

Let’s make this easy. I have a table. visitID studentname dob roll.no value displIndex

  • 0

Let’s make this easy.

I have a table.

visitID   studentname      dob       roll.no     value    displIndex     propID
200019      rob         05/18/1937     101      smoking       2             83
200019      rob         05/18/1937     101                    2             91
200019      rob         05/18/1937     101      alcohol       1             83
200019      rob         05/18/1937     101                    1             91
200020      henry       08/20/1987     102      smoking       2             83
200020      henry       08/20/1987     102                    2             91
200021      king        09/21/1982     103      alcohol       1             83
200021      king        09/21/1982     103                    1             91

I have a table of a similar kind.

Now what I am trying to do is if there is no display index of 1 for that particular visitID of that student, then in the case statement return a value of '779'.

select visitID,studentname
       ,dob
       ,roll.no
       ,value
       ,dispIndex
       ,propID
       ,case 
             WHEN (PATINDEX('%smoking%',value) >0 OR
                  (PATINDEX('%sometimes smoking%',value) > 0) THEN
                        '777'
              WHEN (PATINDEX(%not smoking%,value) >0 OR
                   (PATINDEX(%do not smoke%,value) >0_  THEN
                          '778'
             WHEN (ISNULL(CAST(value as varchar(max)),'')='') THEN
                          '779'
              ELSE
                          '779'
             END VALUE

Here King doesn’t have a record under smoking. So I want it to go into the 779 bucket. How can I perform that?

  • 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-24T22:21:06+00:00Added an answer on May 24, 2026 at 10:21 pm

    Not sure why you’re doing a cast here (based on your sample data), so change this:

    WHEN (ISNULL(CAST(value as varchar(max)),'')='') THEN '779'
    

    To this:

    WHEN ISNULL(value,'') = '' THEN '779'
    

    Also — realize that the way you’re doing your wildcards, all of your ‘not smoking’ values are going to be ‘777’ because they match the first pattern (PATINDEX('%smoking%',value) (i.e., the 2nd pattern won’t even be checked).

    EDIT
    answers to your questions in the comment:


    1) How will it pull the record of king–>smoking–>displayIndex 2?
    Answer: There is no record in your sample set for King with a displayindex of 2. You can’t pull a record that isn’t there.

    2) I want to check for … alcohol records.
    Answer: You already solved that problem for the smokers & non-smokers. Just do the same kind of thing for alchohol. For example: WHEN PATINDEX('%alchohol%',value) > 0 THEN '779'

    3) I would like to put the king student under 779
    Answer: He is. The way a CASE statement works, if none of the WHEN conditions evaluate to true (i.e., ...WHEN PATINDEX('%smoking%',value) > 0) then the ELSE statement is used. Since none of the WHEN statements will evaluate to true for King, he will be under 779.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have table with column 'URL' whrere I store urls like this
Let's suppose I have this piece of code. foreach(string value in myList) { string
Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have some text as follows: do this, do that, then this,
Let's see if I make myself clear. I have an old set of scripts
Let's suppose that we have multi-site CMS and every website in this CMS having
Let's say that I have a set of relations that looks like this: relations

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.