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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:02:23+00:00 2026-06-11T08:02:23+00:00

I am not sure how to do write query in sql. This is what

  • 0

I am not sure how to do write query in sql.


This is what I have tried so far.

where
  case 
       when a.book_id like 'AB%' then a.book_id = b.school_id,   --1   
       when a.book_id like 'CB%' then a.book_id = b.college_id.  --2
  end

case 1 and 2 explanation.

1- I believe is good.
2- when a.book start with letter CB e.g CBQ123 then just take Q123 = b.college_id.

b.college_id don’t have CB in front of it.

Edit to add example

select 
       a.Name,
       a.ID,
       a.Due,
       b.school_id,
       b.college_id
from Student a and FinishedStudent b
where
  case 
       when a.book_id like 'AB%' then a.book_id = b.school_id,   --1   
       when a.book_id like 'CB%' then a.book_id = b.college_id.  --2
  end

if a.book = CBQ111 then Q111 is in FinishedStudent table not CBQ11,so I need to just compare last 3 character.

edit with case 2 example

   when a.book_id ='CBQ111' then a.book_id(Q111) = b.college_id.  --2
  • 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-11T08:02:25+00:00Added an answer on June 11, 2026 at 8:02 am

    Just a minor syntax issue. In T-SQL, CASE is an expression that returns a value, so you need to compare the output to something – it is not a control-of-flow statement like it is in other languages like VB.

    where a.book_id = case 
           when a.book_id like 'AB%' then b.school_id    --1   
           when a.book_id like 'CB%' then b.college_id   --2
      end
    

    Based on the scrambling of word problems added in comments, perhaps what you’re actually after is this:

    WHERE (a.book_id LIKE 'AB%' AND a.book_id = b.school_id)
       OR (a.book_id LIKE 'CB%' AND SUBSTRING(a.book_id, 3, 255) = b.college_id)
    

    There are other ways to write that last line but if book_id has an index the LIKE filter is probably still helpful.

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

Sidebar

Related Questions

I'm not sure how to write this query in SQL. there are two tables
I have the following SQL query: SELECT COUNT(CASE WHEN u.account_new = 1 THEN u.user_id
Hey guys I'm not sure how to write this in AS3, but basically I
Not sure this is possible, but looking to write a script that would return
Not sure how to ask this, but i'll give it a try: I have
I have another SQL/access 2007 question that seems really basic but I'm not sure
I would like to write a query on a sql 2008 that will report
I have some pretty complex reports to write. Some of them... I'm not sure
Not sure whats going on here, or what could be the integer in this
Not sure if this is a big deal. But wondering why when the site

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.