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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:42:18+00:00 2026-05-29T22:42:18+00:00

I need to compare multiple columns from then same row in a table For

  • 0

I need to compare multiple columns from then same row in a table
For example
I have on a row


1 | name | surname | phone | nameWeb | surnameWeb | phoneWeb …


and I need to compare data from DB and data from Web [….Web] columns
something like this


name | nameWeb


surname | surnameWeb


phone | phoneWeb
…


I did this with temporary tables and multiple inserts but I need an optimized solusition because I have a lot of columns

my code

SELECT    
 -- Contatto
                c.id , 
 -- Ditta
                d.nome 'nomeDitta' ,
                d.filiale ,
                d.webNome webNomeDitta ,
                d.webDescrizione webDescrizione ,
-- Persona  
                p.nome Nome ,
                p.cognome Cognome ,
                p.email ,
                p.telefono ,
                p.fax ,
                p.webNome ,
                p.webCognome ,
                p.webEmail ,
                p.webTelefono ,
                p.webFax ,
                p.webNoteAggiuntive ,
                p.canali ,
-- Indirizzo
                i.indirizzo1 ,
                i.indirizzo2 ,
                i.cap ,
                i.localita ,
                i.webIndirizzo1 ,
                i.webIndirizzo2 ,
                i.webCap ,
                i.webLocalita ,
                i.webNome 'NomeInd' ,
-- Nazione
                n.stato 'Nazione' ,
                n2.stato 'webNazione' ,
-- Lingua
                L.nome 'webLingua'
        INTO    #webCont
        FROM    dbo.contatto c ...
        WHERE   c.id = @idContatto

        DECLARE @result TABLE ( ColumnName  NVARCHAR(100) ,
                                DB          NVARCHAR(100) ,
                                Web         NVARCHAR(100) ,
                                hasData     BIT)

        INSERT  INTO @result    SELECT  'Nome Ditta' , nomeDitta , webNomeDitta , @hasData   FROM    #webCont
        INSERT  INTO @result    SELECT  'Nome' , Nome , webNome , @hasData    FROM    #webCont
        INSERT  INTO @result    SELECT  'Cognome' , Cognome , webCognome , @hasData    FROM    #webCont
        INSERT  INTO @result    SELECT  'eMail' , email , webEmail , @hasData    FROM    #webCont
        INSERT  INTO @result    SELECT  'Telefono' , telefono , webTelefono , @hasData    FROM    #webCont
        INSERT  INTO @result    SELECT  'Fax' , fax , webFax , @hasData    FROM    #webCont
        INSERT  INTO @result    SELECT  'Indirizzo 1' , indirizzo1 , webIndirizzo1 , @hasData    FROM    #webCont
        INSERT  INTO @result    SELECT  'Indirizzo 2' , indirizzo2 , webIndirizzo2 , @hasData    FROM    #webCont
        INSERT  INTO @result    SELECT  'Cap' , cap , webCap , @hasData    FROM    #webCont
        INSERT  INTO @result    SELECT  'Localita' , localita , webLocalita , @hasData    FROM    #webCont
        INSERT  INTO @result    SELECT  'Nazione' , Nazione , webNazione , @hasData    FROM    #webCont

Thank you ,
Marian

  • 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-29T22:42:20+00:00Added an answer on May 29, 2026 at 10:42 pm

    Ok, I tried to compile a fairly detailed example for you. What this will do, is build up a dynamic query by:

    1. Finding all columns in the specific table NOT ending in “Web”
    2. Finding all columns in the specific table that DO end in “Web”
    3. Join those two result-sets together and construct a test to measure equality
    4. Use the combined result-set to construct a dynamic query that will execute and perform the necessary matches across all tables that might have a “%Web” counterpart.

    Obviously, this solution won’t work 100% for you, but might guide you into the solution you need. So, without any further ado, I present THE CODE:

    DECLARE 
        @SQL nvarchar(max)
      , @TableName nvarchar(max)
    
    SET @TableName = 'Test'
    
    SET @SQL = 'SELECT '
    ;WITH NonWeb AS
    (
    SELECT
        [COLUMN_NAME] as [NonWebColumn]
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE 
        [TABLE_NAME] = @TableName
    AND [COLUMN_NAME] NOT LIKE '%Web'
    ),
    Web AS
    (
    SELECT
        '[' + C.[COLUMN_NAME] + ']' as [WebColumn]
      , '[' + NonWeb.[NonWebColumn] + ']' as [NonWebColumn]
      , 'CASE WHEN [' + C.[COLUMN_NAME] + '] = [' + NonWeb.[NonWebColumn] + '] THEN ''Equal'' ELSE ''Not Equal'' END as [' + NonWeb.[NonWebColumn] + 'Match]' as [Match]
    FROM INFORMATION_SCHEMA.COLUMNS C
        INNER JOIN NonWeb ON
            REPLACE(C.[COLUMN_NAME],'Web','') = NonWeb.[NonWebColumn]
    WHERE 
        [TABLE_NAME] = @TableName
    AND [COLUMN_NAME] LIKE '%Web'
    )
    
    SELECT @SQL = 'SELECT ' + STUFF
    (
        (
            SELECT
                ', ' + [NonWebColumn] + ', ' + [WebColumn] + ', ' + [Match]
            FROM Web
            FOR XML PATH('')
        )
      , 1
      , 2
      , ''
    ) + ' FROM ' + @TableName
    
    EXEC sp_ExecuteSql @SQL
    

    You can just change the value of the @TableName variable (which gets set at the beginning) to whatever the name of your specific table is and test it out. The assumption here is of course that there are columns that end in “Web” in that specific table…

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

Sidebar

Related Questions

bash master needed... To compare mysqldumps from multiple dates, I need sql GROUP BY,
I have data from two spreadsheets where I need to compare the quantities of
I have multiple foreach's parsing data that I need to compare with each other.
How can you compare multiple input in the same form if they ALL have
I need to compare 2 strings in C# and treat accented letters the same
I need to compare dozens of fields in two objects (instances of the same
I have two documents with a simple schema that I need to compare: current
I need to write a code snippet that would compare multiple arrays and produce
I'm making an App that will have multiple, identical, objects and I need to
How do you compare enums that have multiple bits set? I must be missing

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.