Check if same position characters are equal in two strings












-2















I have a table like below



enter image description here



and i want to get the results of table if any of the characters in same position are equal



example:



fullname: ANURADHA KONGARI
names: AK



withoutinitials: AI



here in the example first letter of AK and first letter of AI are equal. so it should return the result.



I have tried using substring but it results all the records. Because of the length



example of what i have tried



select fullname, names, withoutinitials from #localtable where substring(names,1,1)= substring(withoutinitials,1,1)
or
substring(names,2,1)= substring(withoutinitials,2,1)
or
substring(names,3,1)= substring(withoutinitials,3,1)


Here is a way I tried. It is working but what if when the string length is greater 4



create table #lt2(fullname varchar(100))
insert into #lt2 select getName=case
when len(names)=1 and substring(names,1,1) = substring(withoutinitials,1,1)
then fullname
when len(names)=2 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)))
then fullname
when len(names)=3 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)) or (substring(names,2,1) = substring(withoutinitials,2,1)))
then fullname
when len(names)=4 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)) or (substring(names,2,1) = substring(withoutinitials,2,1))
or (substring(names,3,1) = substring(withoutinitials,3,1)))
then fullname else ''
end
from #localtable

select * from #lt2 where fullname!=''


But this results all the records because some records may not have length 3 so, it even returns the names with length 1.



I want to get the results if atleast one character of two strings are equal in same position.




like 1st position of string1 = 1st position of string 2 or 2nd
position of string1 = 2nd position of string 2 in a generalized way.




Thank you.










share|improve this question

























  • write a scalar function to check the strings by looping through the legnth of the shorter string and checking each character. if a match comes up, return true, but otherwise the loop ends and you return false.

    – Cato
    Nov 20 '18 at 13:24











  • @Cato I tried to do, but its showing an error showplan permission denied

    – chintuyadavsara
    Nov 20 '18 at 13:25











  • have you got the SQL of the function that you wrote?

    – Cato
    Nov 20 '18 at 13:57











  • @Cato sorry, I have deleted it ! but i tried naive approach . I want it to work for strings of any length. Please see the updated question

    – chintuyadavsara
    Nov 20 '18 at 14:00











  • your 'showplan permission' sounds like something to do with the permission to see the query execution plan, as opposed to the function being in error

    – Cato
    Nov 20 '18 at 14:27
















-2















I have a table like below



enter image description here



and i want to get the results of table if any of the characters in same position are equal



example:



fullname: ANURADHA KONGARI
names: AK



withoutinitials: AI



here in the example first letter of AK and first letter of AI are equal. so it should return the result.



I have tried using substring but it results all the records. Because of the length



example of what i have tried



select fullname, names, withoutinitials from #localtable where substring(names,1,1)= substring(withoutinitials,1,1)
or
substring(names,2,1)= substring(withoutinitials,2,1)
or
substring(names,3,1)= substring(withoutinitials,3,1)


Here is a way I tried. It is working but what if when the string length is greater 4



create table #lt2(fullname varchar(100))
insert into #lt2 select getName=case
when len(names)=1 and substring(names,1,1) = substring(withoutinitials,1,1)
then fullname
when len(names)=2 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)))
then fullname
when len(names)=3 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)) or (substring(names,2,1) = substring(withoutinitials,2,1)))
then fullname
when len(names)=4 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)) or (substring(names,2,1) = substring(withoutinitials,2,1))
or (substring(names,3,1) = substring(withoutinitials,3,1)))
then fullname else ''
end
from #localtable

select * from #lt2 where fullname!=''


But this results all the records because some records may not have length 3 so, it even returns the names with length 1.



I want to get the results if atleast one character of two strings are equal in same position.




like 1st position of string1 = 1st position of string 2 or 2nd
position of string1 = 2nd position of string 2 in a generalized way.




Thank you.










share|improve this question

























  • write a scalar function to check the strings by looping through the legnth of the shorter string and checking each character. if a match comes up, return true, but otherwise the loop ends and you return false.

    – Cato
    Nov 20 '18 at 13:24











  • @Cato I tried to do, but its showing an error showplan permission denied

    – chintuyadavsara
    Nov 20 '18 at 13:25











  • have you got the SQL of the function that you wrote?

    – Cato
    Nov 20 '18 at 13:57











  • @Cato sorry, I have deleted it ! but i tried naive approach . I want it to work for strings of any length. Please see the updated question

    – chintuyadavsara
    Nov 20 '18 at 14:00











  • your 'showplan permission' sounds like something to do with the permission to see the query execution plan, as opposed to the function being in error

    – Cato
    Nov 20 '18 at 14:27














-2












-2








-2








I have a table like below



enter image description here



and i want to get the results of table if any of the characters in same position are equal



example:



fullname: ANURADHA KONGARI
names: AK



withoutinitials: AI



here in the example first letter of AK and first letter of AI are equal. so it should return the result.



I have tried using substring but it results all the records. Because of the length



example of what i have tried



select fullname, names, withoutinitials from #localtable where substring(names,1,1)= substring(withoutinitials,1,1)
or
substring(names,2,1)= substring(withoutinitials,2,1)
or
substring(names,3,1)= substring(withoutinitials,3,1)


Here is a way I tried. It is working but what if when the string length is greater 4



create table #lt2(fullname varchar(100))
insert into #lt2 select getName=case
when len(names)=1 and substring(names,1,1) = substring(withoutinitials,1,1)
then fullname
when len(names)=2 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)))
then fullname
when len(names)=3 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)) or (substring(names,2,1) = substring(withoutinitials,2,1)))
then fullname
when len(names)=4 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)) or (substring(names,2,1) = substring(withoutinitials,2,1))
or (substring(names,3,1) = substring(withoutinitials,3,1)))
then fullname else ''
end
from #localtable

select * from #lt2 where fullname!=''


But this results all the records because some records may not have length 3 so, it even returns the names with length 1.



I want to get the results if atleast one character of two strings are equal in same position.




like 1st position of string1 = 1st position of string 2 or 2nd
position of string1 = 2nd position of string 2 in a generalized way.




Thank you.










share|improve this question
















I have a table like below



enter image description here



and i want to get the results of table if any of the characters in same position are equal



example:



fullname: ANURADHA KONGARI
names: AK



withoutinitials: AI



here in the example first letter of AK and first letter of AI are equal. so it should return the result.



I have tried using substring but it results all the records. Because of the length



example of what i have tried



select fullname, names, withoutinitials from #localtable where substring(names,1,1)= substring(withoutinitials,1,1)
or
substring(names,2,1)= substring(withoutinitials,2,1)
or
substring(names,3,1)= substring(withoutinitials,3,1)


Here is a way I tried. It is working but what if when the string length is greater 4



create table #lt2(fullname varchar(100))
insert into #lt2 select getName=case
when len(names)=1 and substring(names,1,1) = substring(withoutinitials,1,1)
then fullname
when len(names)=2 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)))
then fullname
when len(names)=3 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)) or (substring(names,2,1) = substring(withoutinitials,2,1)))
then fullname
when len(names)=4 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)) or (substring(names,2,1) = substring(withoutinitials,2,1))
or (substring(names,3,1) = substring(withoutinitials,3,1)))
then fullname else ''
end
from #localtable

select * from #lt2 where fullname!=''


But this results all the records because some records may not have length 3 so, it even returns the names with length 1.



I want to get the results if atleast one character of two strings are equal in same position.




like 1st position of string1 = 1st position of string 2 or 2nd
position of string1 = 2nd position of string 2 in a generalized way.




Thank you.







sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 14:01







chintuyadavsara

















asked Nov 20 '18 at 13:21









chintuyadavsarachintuyadavsara

1,1241415




1,1241415













  • write a scalar function to check the strings by looping through the legnth of the shorter string and checking each character. if a match comes up, return true, but otherwise the loop ends and you return false.

    – Cato
    Nov 20 '18 at 13:24











  • @Cato I tried to do, but its showing an error showplan permission denied

    – chintuyadavsara
    Nov 20 '18 at 13:25











  • have you got the SQL of the function that you wrote?

    – Cato
    Nov 20 '18 at 13:57











  • @Cato sorry, I have deleted it ! but i tried naive approach . I want it to work for strings of any length. Please see the updated question

    – chintuyadavsara
    Nov 20 '18 at 14:00











  • your 'showplan permission' sounds like something to do with the permission to see the query execution plan, as opposed to the function being in error

    – Cato
    Nov 20 '18 at 14:27



















  • write a scalar function to check the strings by looping through the legnth of the shorter string and checking each character. if a match comes up, return true, but otherwise the loop ends and you return false.

    – Cato
    Nov 20 '18 at 13:24











  • @Cato I tried to do, but its showing an error showplan permission denied

    – chintuyadavsara
    Nov 20 '18 at 13:25











  • have you got the SQL of the function that you wrote?

    – Cato
    Nov 20 '18 at 13:57











  • @Cato sorry, I have deleted it ! but i tried naive approach . I want it to work for strings of any length. Please see the updated question

    – chintuyadavsara
    Nov 20 '18 at 14:00











  • your 'showplan permission' sounds like something to do with the permission to see the query execution plan, as opposed to the function being in error

    – Cato
    Nov 20 '18 at 14:27

















write a scalar function to check the strings by looping through the legnth of the shorter string and checking each character. if a match comes up, return true, but otherwise the loop ends and you return false.

– Cato
Nov 20 '18 at 13:24





write a scalar function to check the strings by looping through the legnth of the shorter string and checking each character. if a match comes up, return true, but otherwise the loop ends and you return false.

– Cato
Nov 20 '18 at 13:24













@Cato I tried to do, but its showing an error showplan permission denied

– chintuyadavsara
Nov 20 '18 at 13:25





@Cato I tried to do, but its showing an error showplan permission denied

– chintuyadavsara
Nov 20 '18 at 13:25













have you got the SQL of the function that you wrote?

– Cato
Nov 20 '18 at 13:57





have you got the SQL of the function that you wrote?

– Cato
Nov 20 '18 at 13:57













@Cato sorry, I have deleted it ! but i tried naive approach . I want it to work for strings of any length. Please see the updated question

– chintuyadavsara
Nov 20 '18 at 14:00





@Cato sorry, I have deleted it ! but i tried naive approach . I want it to work for strings of any length. Please see the updated question

– chintuyadavsara
Nov 20 '18 at 14:00













your 'showplan permission' sounds like something to do with the permission to see the query execution plan, as opposed to the function being in error

– Cato
Nov 20 '18 at 14:27





your 'showplan permission' sounds like something to do with the permission to see the query execution plan, as opposed to the function being in error

– Cato
Nov 20 '18 at 14:27












3 Answers
3






active

oldest

votes


















0














You need to use 1 and 2 for your positions, and add an additional check for blank spaces when there are not two characters.



declare @table table (fullname varchar(64), names varchar(2), withoutinititals varchar(2))
insert into @table
values
('GANGA RAJAM','GR','AM'),
('ANURADHA KONGARI','AK','AI'),
('PATEL SHIVAJI','R','H'),
('NEW NAME','X','X')

select
*
,substring(names,1,1)
,substring(names,2,1)
,substring(withoutinititals,1,1)
,substring(withoutinititals,2,1)
from @table
where
(substring(names,1,1) = substring(withoutinititals,1,1))
or
(substring(names,2,1) = substring(withoutinititals,2,1) and substring(withoutinititals,2,1) != '')


Notice if you remove this part of the where clause, substring(withoutinititals,2,1) != '', you will get false positives






share|improve this answer


























  • Thank you, But What if the string lenght i.e, names length is greater than 2? do i need to write the substring again?

    – chintuyadavsara
    Nov 20 '18 at 14:04













  • if you are trying to check every single letter against every letter then yes, you need to use a loop ideally a full text index

    – scsimon
    Nov 20 '18 at 14:05











  • Yeah! That is what i want, but i failed to do so and I dont have permissions to create a function :(

    – chintuyadavsara
    Nov 20 '18 at 14:06






  • 1





    ask John above for some XML magic... it's pretty good at that

    – scsimon
    Nov 20 '18 at 14:07






  • 1





    @scsimon Thanks for the chuckle

    – John Cappelletti
    Nov 20 '18 at 14:12



















0














Right pad with unequal chars '0' for names and '1' for withoutinitials:



select fullname, names, withoutinitials from #localtable where 
substring(names, 1, 1) = substring(withoutinitials, 1, 1)
or
substring(LEFT(CONCAT(names, '0'), 2), 2, 1) = substring(LEFT(CONCAT(withoutinitials, '1'), 2), 2, 1)
or
substring(LEFT(CONCAT(names, '00'), 3), 3, 1) = substring(LEFT(CONCAT(withoutinitials, '11'), 3), 3, 1)
or
substring(LEFT(CONCAT(names, '000'), 4), 4, 1) = substring(LEFT(CONCAT(withoutinitials, '111'), 4), 4, 1)




with sample data:



INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc1', 'GR', 'AM');
INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc2', 'G', 'A');
INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc3', 'GR', 'GM');
INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc4', 'R', 'R');
INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc5', 'GRAA', 'AMAM');
INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc6', 'GRS', 'AMS');
INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc7', 'AGR', 'AAM');
INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc8', 'GR', 'AM');


the result is:



    fullname    names   withoutinitials
1 abc3 GR GM
2 abc4 R R
3 abc5 GRAA AMAM
4 abc6 GRS AMS
5 abc7 AGR AAM





share|improve this answer

































    0














    just a bit of fun, but I tried a recursive CTE



    --select distinct top 4000 employeeid, surname, ForeName1 forename into #test from isEmployeeMaster where Sequence  = 1 



    ;WITH S AS (SELECT 1 AS A, M.employeeid as E, M.Surname as sur, SUBSTRING(M.surname,1,1) ATOM FROM #test M
    UNION ALL
    SELECT A + 1, E, sur, SUBSTRING(S.sur,A+1,1) FROM S WHERE A < LEN(S.sur)
    ),
    F AS (SELECT 1 AS A, M2.employeeid as E, M2.forename as fore, SUBSTRING(M2.forename,1,1) ATOM FROM #test M2
    UNION ALL
    SELECT A + 1, E, fore, SUBSTRING(f.fore,A+1,1) FROM f WHERE A < LEN(f.fore)
    )

    select t.* from #test t where t.EmployeeId in
    (select s.e from S join F on S.E = F.E and S.Atom = F.Atom and S.A = F.a)



    --drop table #test





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393940%2fcheck-if-same-position-characters-are-equal-in-two-strings%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You need to use 1 and 2 for your positions, and add an additional check for blank spaces when there are not two characters.



      declare @table table (fullname varchar(64), names varchar(2), withoutinititals varchar(2))
      insert into @table
      values
      ('GANGA RAJAM','GR','AM'),
      ('ANURADHA KONGARI','AK','AI'),
      ('PATEL SHIVAJI','R','H'),
      ('NEW NAME','X','X')

      select
      *
      ,substring(names,1,1)
      ,substring(names,2,1)
      ,substring(withoutinititals,1,1)
      ,substring(withoutinititals,2,1)
      from @table
      where
      (substring(names,1,1) = substring(withoutinititals,1,1))
      or
      (substring(names,2,1) = substring(withoutinititals,2,1) and substring(withoutinititals,2,1) != '')


      Notice if you remove this part of the where clause, substring(withoutinititals,2,1) != '', you will get false positives






      share|improve this answer


























      • Thank you, But What if the string lenght i.e, names length is greater than 2? do i need to write the substring again?

        – chintuyadavsara
        Nov 20 '18 at 14:04













      • if you are trying to check every single letter against every letter then yes, you need to use a loop ideally a full text index

        – scsimon
        Nov 20 '18 at 14:05











      • Yeah! That is what i want, but i failed to do so and I dont have permissions to create a function :(

        – chintuyadavsara
        Nov 20 '18 at 14:06






      • 1





        ask John above for some XML magic... it's pretty good at that

        – scsimon
        Nov 20 '18 at 14:07






      • 1





        @scsimon Thanks for the chuckle

        – John Cappelletti
        Nov 20 '18 at 14:12
















      0














      You need to use 1 and 2 for your positions, and add an additional check for blank spaces when there are not two characters.



      declare @table table (fullname varchar(64), names varchar(2), withoutinititals varchar(2))
      insert into @table
      values
      ('GANGA RAJAM','GR','AM'),
      ('ANURADHA KONGARI','AK','AI'),
      ('PATEL SHIVAJI','R','H'),
      ('NEW NAME','X','X')

      select
      *
      ,substring(names,1,1)
      ,substring(names,2,1)
      ,substring(withoutinititals,1,1)
      ,substring(withoutinititals,2,1)
      from @table
      where
      (substring(names,1,1) = substring(withoutinititals,1,1))
      or
      (substring(names,2,1) = substring(withoutinititals,2,1) and substring(withoutinititals,2,1) != '')


      Notice if you remove this part of the where clause, substring(withoutinititals,2,1) != '', you will get false positives






      share|improve this answer


























      • Thank you, But What if the string lenght i.e, names length is greater than 2? do i need to write the substring again?

        – chintuyadavsara
        Nov 20 '18 at 14:04













      • if you are trying to check every single letter against every letter then yes, you need to use a loop ideally a full text index

        – scsimon
        Nov 20 '18 at 14:05











      • Yeah! That is what i want, but i failed to do so and I dont have permissions to create a function :(

        – chintuyadavsara
        Nov 20 '18 at 14:06






      • 1





        ask John above for some XML magic... it's pretty good at that

        – scsimon
        Nov 20 '18 at 14:07






      • 1





        @scsimon Thanks for the chuckle

        – John Cappelletti
        Nov 20 '18 at 14:12














      0












      0








      0







      You need to use 1 and 2 for your positions, and add an additional check for blank spaces when there are not two characters.



      declare @table table (fullname varchar(64), names varchar(2), withoutinititals varchar(2))
      insert into @table
      values
      ('GANGA RAJAM','GR','AM'),
      ('ANURADHA KONGARI','AK','AI'),
      ('PATEL SHIVAJI','R','H'),
      ('NEW NAME','X','X')

      select
      *
      ,substring(names,1,1)
      ,substring(names,2,1)
      ,substring(withoutinititals,1,1)
      ,substring(withoutinititals,2,1)
      from @table
      where
      (substring(names,1,1) = substring(withoutinititals,1,1))
      or
      (substring(names,2,1) = substring(withoutinititals,2,1) and substring(withoutinititals,2,1) != '')


      Notice if you remove this part of the where clause, substring(withoutinititals,2,1) != '', you will get false positives






      share|improve this answer















      You need to use 1 and 2 for your positions, and add an additional check for blank spaces when there are not two characters.



      declare @table table (fullname varchar(64), names varchar(2), withoutinititals varchar(2))
      insert into @table
      values
      ('GANGA RAJAM','GR','AM'),
      ('ANURADHA KONGARI','AK','AI'),
      ('PATEL SHIVAJI','R','H'),
      ('NEW NAME','X','X')

      select
      *
      ,substring(names,1,1)
      ,substring(names,2,1)
      ,substring(withoutinititals,1,1)
      ,substring(withoutinititals,2,1)
      from @table
      where
      (substring(names,1,1) = substring(withoutinititals,1,1))
      or
      (substring(names,2,1) = substring(withoutinititals,2,1) and substring(withoutinititals,2,1) != '')


      Notice if you remove this part of the where clause, substring(withoutinititals,2,1) != '', you will get false positives







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 20 '18 at 14:04

























      answered Nov 20 '18 at 14:03









      scsimonscsimon

      22.1k51536




      22.1k51536













      • Thank you, But What if the string lenght i.e, names length is greater than 2? do i need to write the substring again?

        – chintuyadavsara
        Nov 20 '18 at 14:04













      • if you are trying to check every single letter against every letter then yes, you need to use a loop ideally a full text index

        – scsimon
        Nov 20 '18 at 14:05











      • Yeah! That is what i want, but i failed to do so and I dont have permissions to create a function :(

        – chintuyadavsara
        Nov 20 '18 at 14:06






      • 1





        ask John above for some XML magic... it's pretty good at that

        – scsimon
        Nov 20 '18 at 14:07






      • 1





        @scsimon Thanks for the chuckle

        – John Cappelletti
        Nov 20 '18 at 14:12



















      • Thank you, But What if the string lenght i.e, names length is greater than 2? do i need to write the substring again?

        – chintuyadavsara
        Nov 20 '18 at 14:04













      • if you are trying to check every single letter against every letter then yes, you need to use a loop ideally a full text index

        – scsimon
        Nov 20 '18 at 14:05











      • Yeah! That is what i want, but i failed to do so and I dont have permissions to create a function :(

        – chintuyadavsara
        Nov 20 '18 at 14:06






      • 1





        ask John above for some XML magic... it's pretty good at that

        – scsimon
        Nov 20 '18 at 14:07






      • 1





        @scsimon Thanks for the chuckle

        – John Cappelletti
        Nov 20 '18 at 14:12

















      Thank you, But What if the string lenght i.e, names length is greater than 2? do i need to write the substring again?

      – chintuyadavsara
      Nov 20 '18 at 14:04







      Thank you, But What if the string lenght i.e, names length is greater than 2? do i need to write the substring again?

      – chintuyadavsara
      Nov 20 '18 at 14:04















      if you are trying to check every single letter against every letter then yes, you need to use a loop ideally a full text index

      – scsimon
      Nov 20 '18 at 14:05





      if you are trying to check every single letter against every letter then yes, you need to use a loop ideally a full text index

      – scsimon
      Nov 20 '18 at 14:05













      Yeah! That is what i want, but i failed to do so and I dont have permissions to create a function :(

      – chintuyadavsara
      Nov 20 '18 at 14:06





      Yeah! That is what i want, but i failed to do so and I dont have permissions to create a function :(

      – chintuyadavsara
      Nov 20 '18 at 14:06




      1




      1





      ask John above for some XML magic... it's pretty good at that

      – scsimon
      Nov 20 '18 at 14:07





      ask John above for some XML magic... it's pretty good at that

      – scsimon
      Nov 20 '18 at 14:07




      1




      1





      @scsimon Thanks for the chuckle

      – John Cappelletti
      Nov 20 '18 at 14:12





      @scsimon Thanks for the chuckle

      – John Cappelletti
      Nov 20 '18 at 14:12













      0














      Right pad with unequal chars '0' for names and '1' for withoutinitials:



      select fullname, names, withoutinitials from #localtable where 
      substring(names, 1, 1) = substring(withoutinitials, 1, 1)
      or
      substring(LEFT(CONCAT(names, '0'), 2), 2, 1) = substring(LEFT(CONCAT(withoutinitials, '1'), 2), 2, 1)
      or
      substring(LEFT(CONCAT(names, '00'), 3), 3, 1) = substring(LEFT(CONCAT(withoutinitials, '11'), 3), 3, 1)
      or
      substring(LEFT(CONCAT(names, '000'), 4), 4, 1) = substring(LEFT(CONCAT(withoutinitials, '111'), 4), 4, 1)




      with sample data:



      INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc1', 'GR', 'AM');
      INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc2', 'G', 'A');
      INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc3', 'GR', 'GM');
      INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc4', 'R', 'R');
      INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc5', 'GRAA', 'AMAM');
      INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc6', 'GRS', 'AMS');
      INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc7', 'AGR', 'AAM');
      INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc8', 'GR', 'AM');


      the result is:



          fullname    names   withoutinitials
      1 abc3 GR GM
      2 abc4 R R
      3 abc5 GRAA AMAM
      4 abc6 GRS AMS
      5 abc7 AGR AAM





      share|improve this answer






























        0














        Right pad with unequal chars '0' for names and '1' for withoutinitials:



        select fullname, names, withoutinitials from #localtable where 
        substring(names, 1, 1) = substring(withoutinitials, 1, 1)
        or
        substring(LEFT(CONCAT(names, '0'), 2), 2, 1) = substring(LEFT(CONCAT(withoutinitials, '1'), 2), 2, 1)
        or
        substring(LEFT(CONCAT(names, '00'), 3), 3, 1) = substring(LEFT(CONCAT(withoutinitials, '11'), 3), 3, 1)
        or
        substring(LEFT(CONCAT(names, '000'), 4), 4, 1) = substring(LEFT(CONCAT(withoutinitials, '111'), 4), 4, 1)




        with sample data:



        INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc1', 'GR', 'AM');
        INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc2', 'G', 'A');
        INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc3', 'GR', 'GM');
        INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc4', 'R', 'R');
        INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc5', 'GRAA', 'AMAM');
        INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc6', 'GRS', 'AMS');
        INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc7', 'AGR', 'AAM');
        INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc8', 'GR', 'AM');


        the result is:



            fullname    names   withoutinitials
        1 abc3 GR GM
        2 abc4 R R
        3 abc5 GRAA AMAM
        4 abc6 GRS AMS
        5 abc7 AGR AAM





        share|improve this answer




























          0












          0








          0







          Right pad with unequal chars '0' for names and '1' for withoutinitials:



          select fullname, names, withoutinitials from #localtable where 
          substring(names, 1, 1) = substring(withoutinitials, 1, 1)
          or
          substring(LEFT(CONCAT(names, '0'), 2), 2, 1) = substring(LEFT(CONCAT(withoutinitials, '1'), 2), 2, 1)
          or
          substring(LEFT(CONCAT(names, '00'), 3), 3, 1) = substring(LEFT(CONCAT(withoutinitials, '11'), 3), 3, 1)
          or
          substring(LEFT(CONCAT(names, '000'), 4), 4, 1) = substring(LEFT(CONCAT(withoutinitials, '111'), 4), 4, 1)




          with sample data:



          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc1', 'GR', 'AM');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc2', 'G', 'A');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc3', 'GR', 'GM');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc4', 'R', 'R');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc5', 'GRAA', 'AMAM');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc6', 'GRS', 'AMS');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc7', 'AGR', 'AAM');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc8', 'GR', 'AM');


          the result is:



              fullname    names   withoutinitials
          1 abc3 GR GM
          2 abc4 R R
          3 abc5 GRAA AMAM
          4 abc6 GRS AMS
          5 abc7 AGR AAM





          share|improve this answer















          Right pad with unequal chars '0' for names and '1' for withoutinitials:



          select fullname, names, withoutinitials from #localtable where 
          substring(names, 1, 1) = substring(withoutinitials, 1, 1)
          or
          substring(LEFT(CONCAT(names, '0'), 2), 2, 1) = substring(LEFT(CONCAT(withoutinitials, '1'), 2), 2, 1)
          or
          substring(LEFT(CONCAT(names, '00'), 3), 3, 1) = substring(LEFT(CONCAT(withoutinitials, '11'), 3), 3, 1)
          or
          substring(LEFT(CONCAT(names, '000'), 4), 4, 1) = substring(LEFT(CONCAT(withoutinitials, '111'), 4), 4, 1)




          with sample data:



          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc1', 'GR', 'AM');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc2', 'G', 'A');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc3', 'GR', 'GM');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc4', 'R', 'R');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc5', 'GRAA', 'AMAM');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc6', 'GRS', 'AMS');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc7', 'AGR', 'AAM');
          INSERT INTO localtable (fullname, names, withoutinitials) VALUES ('abc8', 'GR', 'AM');


          the result is:



              fullname    names   withoutinitials
          1 abc3 GR GM
          2 abc4 R R
          3 abc5 GRAA AMAM
          4 abc6 GRS AMS
          5 abc7 AGR AAM






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 15:16

























          answered Nov 20 '18 at 14:08









          forpasforpas

          14.3k3624




          14.3k3624























              0














              just a bit of fun, but I tried a recursive CTE



              --select distinct top 4000 employeeid, surname, ForeName1 forename into #test from isEmployeeMaster where Sequence  = 1 



              ;WITH S AS (SELECT 1 AS A, M.employeeid as E, M.Surname as sur, SUBSTRING(M.surname,1,1) ATOM FROM #test M
              UNION ALL
              SELECT A + 1, E, sur, SUBSTRING(S.sur,A+1,1) FROM S WHERE A < LEN(S.sur)
              ),
              F AS (SELECT 1 AS A, M2.employeeid as E, M2.forename as fore, SUBSTRING(M2.forename,1,1) ATOM FROM #test M2
              UNION ALL
              SELECT A + 1, E, fore, SUBSTRING(f.fore,A+1,1) FROM f WHERE A < LEN(f.fore)
              )

              select t.* from #test t where t.EmployeeId in
              (select s.e from S join F on S.E = F.E and S.Atom = F.Atom and S.A = F.a)



              --drop table #test





              share|improve this answer




























                0














                just a bit of fun, but I tried a recursive CTE



                --select distinct top 4000 employeeid, surname, ForeName1 forename into #test from isEmployeeMaster where Sequence  = 1 



                ;WITH S AS (SELECT 1 AS A, M.employeeid as E, M.Surname as sur, SUBSTRING(M.surname,1,1) ATOM FROM #test M
                UNION ALL
                SELECT A + 1, E, sur, SUBSTRING(S.sur,A+1,1) FROM S WHERE A < LEN(S.sur)
                ),
                F AS (SELECT 1 AS A, M2.employeeid as E, M2.forename as fore, SUBSTRING(M2.forename,1,1) ATOM FROM #test M2
                UNION ALL
                SELECT A + 1, E, fore, SUBSTRING(f.fore,A+1,1) FROM f WHERE A < LEN(f.fore)
                )

                select t.* from #test t where t.EmployeeId in
                (select s.e from S join F on S.E = F.E and S.Atom = F.Atom and S.A = F.a)



                --drop table #test





                share|improve this answer


























                  0












                  0








                  0







                  just a bit of fun, but I tried a recursive CTE



                  --select distinct top 4000 employeeid, surname, ForeName1 forename into #test from isEmployeeMaster where Sequence  = 1 



                  ;WITH S AS (SELECT 1 AS A, M.employeeid as E, M.Surname as sur, SUBSTRING(M.surname,1,1) ATOM FROM #test M
                  UNION ALL
                  SELECT A + 1, E, sur, SUBSTRING(S.sur,A+1,1) FROM S WHERE A < LEN(S.sur)
                  ),
                  F AS (SELECT 1 AS A, M2.employeeid as E, M2.forename as fore, SUBSTRING(M2.forename,1,1) ATOM FROM #test M2
                  UNION ALL
                  SELECT A + 1, E, fore, SUBSTRING(f.fore,A+1,1) FROM f WHERE A < LEN(f.fore)
                  )

                  select t.* from #test t where t.EmployeeId in
                  (select s.e from S join F on S.E = F.E and S.Atom = F.Atom and S.A = F.a)



                  --drop table #test





                  share|improve this answer













                  just a bit of fun, but I tried a recursive CTE



                  --select distinct top 4000 employeeid, surname, ForeName1 forename into #test from isEmployeeMaster where Sequence  = 1 



                  ;WITH S AS (SELECT 1 AS A, M.employeeid as E, M.Surname as sur, SUBSTRING(M.surname,1,1) ATOM FROM #test M
                  UNION ALL
                  SELECT A + 1, E, sur, SUBSTRING(S.sur,A+1,1) FROM S WHERE A < LEN(S.sur)
                  ),
                  F AS (SELECT 1 AS A, M2.employeeid as E, M2.forename as fore, SUBSTRING(M2.forename,1,1) ATOM FROM #test M2
                  UNION ALL
                  SELECT A + 1, E, fore, SUBSTRING(f.fore,A+1,1) FROM f WHERE A < LEN(f.fore)
                  )

                  select t.* from #test t where t.EmployeeId in
                  (select s.e from S join F on S.E = F.E and S.Atom = F.Atom and S.A = F.a)



                  --drop table #test






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 15:51









                  CatoCato

                  2,965211




                  2,965211






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393940%2fcheck-if-same-position-characters-are-equal-in-two-strings%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()