I am trying to create a SQL WHERE statement to compare strings that its allowed that maximum one character is different but still must be same length. I have tried using substring search with LIKE command but it does not work well if the char that is different in the middle of the string etc. Strings that should match examples:
should match: "ABC123" and "ABC023" "ABC124" and "ABC125" etc
should not match
"ABC123" and "ACB132"
How can this be achieved with a SQL WHERE statement?
Assuming you are on on the latest version of SQL Server (2022 at time of writing), you could use
GENERATE_SERIESto get a row for each character in your search string, and then useSTUFFto replace each character in a position with a single character wildcard (_). Then you can use anLIKEinside anEXISTS:db<>fiddle
If you aren't in SQL Server 2022+ then you could use your own tally function to achieve the same result.