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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:21:30+00:00 2026-06-07T02:21:30+00:00

I have a question about a specific query I’m writing in a SQL Server

  • 0

I have a question about a specific query I’m writing in a SQL Server 2008 database. This is part of a (much) larger query, but I’ll simplify the hell out of it to make it easier to isolate what I’m trying to do.

I have a table TableX, two columns:

  • IDX (int) identity
  • VarX (varchar(20))

I have two auxiliary tables, TableY and TableZ. Both have two columns,

TableY
  • IDY (int) identity

  • VarY (varchar(20))

    and TableZ

  • IDZ (int) identity

  • VarZ (varchar(20))

I am doing an insert into a fourth table, we’ll call it TableA. TableA has three columns:

  • IDA (int) identity
  • IDY (int)
  • IDZ (int)

The IDY and IDZ fields in this table are foreign keys to the identity columns in Table Y and Table Z.

Sounds complicated, but its a really simple connection. Here’s where it gets fun though.

So the data in TableX might look like this:

IDX | VarX
--------------
1   | ABC
2   | ABC-LMN

The data in TableY:

IDY | VarY
----------------
1   |ABC
2   | HIJ

The data in TableZ:

IDZ | VarZ
----------------
1   | LMN
2   | OPQ

Basically, VarX (the second column), is either going to be a single varchar string, or two connected by a hyphen, no spaces. In the case of a single string, with no hyphen, its always a match in Table Y. The match would be on VarY in TableY = VarX in TableX. I would grab the associated ID (IDY), and use that in the insert on TableA.

In the example of row 1, the insert into TableA would be (null, 1, null)

Now in the case of row 2, it has two strings separated by a hyphen. So the insert into TableA would ultimately be (null, 1, 1).

So my question is this… how do I formulate the insert with its corresponding joins to deal with this type of logic? I’m sure it has to be a case statement… just having alot of trouble visualizing the full query, as I’m no DBA…. Any help is appreciated.

  • 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-07T02:21:31+00:00Added an answer on June 7, 2026 at 2:21 am

    There’s no need to make this dynamic SQL, unless the proc this is part of requires it.

    INSERT INTO TableA (IDY,IDZ)
    SELECT ty.IDY, tz.IDZ
    FROM TableX tx
    LEFT JOIN TableY ty ON ty.VarY =
       (CASE WHEN CHARINDEX('-', tx.VarX) = 0 THEN tx.VarX
       ELSE LEFT(tx.VarX, CHARINDEX('-', tx.VarX) - 1) END)
    LEFT JOIN TableZ tz ON tz.VarZ =
       CASE WHEN CHARINDEX('-', tx.VarX) = 0 THEN NULL
       ELSE RIGHT(tx.VarX, LEN(tx.VarX) - CHARINDEX('-', tx.VarX)) END
    

    I have intentionally omitted inserting NULL in to TableA.IDA, as I don’t know why you’d want to try inserting NULL in to an identity column, or if that’s even possible. I have also assumed that the data in VarX will not have degenerate cases, such as '-', 'ZXC-', 'ASD-BNM ', etc.

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

Sidebar

Related Questions

I have a question about SQL Server indexes. I'm not a DBA and assume
Hello guys I have a specific question about the AND clause in SQL. The
I have a quick question about encapsulating specific types with typedef . Say I
I have a question about whether or not a specific way of applying of
I have a fairly specific question about concurrent programming in C. I have done
this is not a question about a specific programming problem, it's about examining different
This question is about a general technique in SQL, that I can't quite work
This is a bit of subjective question about a specific situation. Main goal for
This question os partially a Drupal question and partially a SQL question. I have
I have a table in SQL Server 2000 that I am trying to query

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.