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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:58:57+00:00 2026-06-12T07:58:57+00:00

My previous question was – insert substring into new column I have two cases

  • 0

My previous question was – insert substring into new column

I have two cases I want to run on the same column.

The first –

SET [ref_id] = CASE 
WHEN CHARINDEX('-', [full_id]) = 4 
THEN SUBSTRING([full_id], 1, 3) ELSE NULL

The second –

SET [ref_id] = CASE 
WHEN CHARINDEX('ZZZ-', [full_id]) = 1 
THEN SUBSTRING([full_id], 5, 3) ELSE NULL

My [full_id] column data format is mainly ABC-123D2-45FG67 BUT some of it is all numbers or is prefixed with ZZZ-

If the data is only numbers I want the [ref_id] column to be NULL.

If the data is prefixed with ZZZ- I want that part to be skipped.

If the data is not ZZZ- and starts with ABC- I want to copy the ABC (in some cases there is AB-1234 and that should be NULL).

My code works fine except that the two strings do not function together and when I run them separately they cancel out the work of the previous query.

How can I run both and end up with NULLS only for the data in formats like 123456 and AB-1234

Thank you for the help.

  • 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-12T07:58:59+00:00Added an answer on June 12, 2026 at 7:58 am

    Isn’t this just a case (hah!) of nesting your case statements?

    SET [ref_id] = CASE WHEN CHARINDEX('ZZZ-', [full_id]) = 1 THEN SUBSTRING([full_id], 5, 3) ELSE 
      (CASE WHEN CHARINDEX('-', [full_id]) = 4 THEN SUBSTRING([full_id], 1, 3) ELSE NULL end ) end
    

    Edit: Removed “else null” towards end. Think that’s better. I don’t have a server available to me to test.

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

Sidebar

Related Questions

My previous question was answered here: Two floating divs side by side, same height
My previous question on the same theme: C#: Asynchronous NamedPipeServerStream understanding Now I have
Previous question was to encode two Int32 into one Int64 [ C# - Making
This question is related to my previous question . The storyline: I have an
New question As suggested by Asaph in previous question: Regex to check if exact
This question is similar to my previous question but not the same ... please
Further to my previous question (qv) ... I have already created the table(s) and
Please use my previous question as reference. I have marked an answer already. Question
Following on from a previous question, I have the following code: var results =
(This question is loosely related to my previous question ) Hello there. I have

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.