How to determine if a hyphen (-) exists inside a columnAlternatives to LIKE and FTS for mid name searchIndexing strategy for dynamic predicateQuery to match exact column or partial data of the column: SQL Server / AzurePerforming search on a column by queryPerform regex search queries of a text column in table of 100+ million using regexDoes there exist a way to determine the exact file that contains an allocation unit in a filegroup of multiple files?How long does it take a full text index to update after inserting a large amount of text?Substring without the first n charactersCan't import flat file into SqlServerSearching for and parsing out specific text parts within the values of a column
How can I get an unreasonable manager to approve time off?
Where Mongol herds graze
Overlapping String-Blocks
Déjà vu, again?
Why is one of Madera Municipal's runways labelled with only "R" on both sides?
Were Alexander the Great and Hephaestion lovers?
Why is the tail group of virtually every airplane swept instead of straight?
How to forge a multi-part weapon?
Share calendar details request from manager's manager
Why would future John risk sending back a T-800 to save his younger self?
Is it possible to 'live off the sea'
A curious prime counting approximation or just data overfitting?
What to do when surprise and a high initiative roll conflict with the narrative?
Should I avoid hard-packed crusher dust trails with my hybrid?
How can "научись" mean "take it and keep trying"?
Find the limit of a multiplying term function when n tends to infinity.
Would the US government be able to hold control if all electronics were disabled for an indefinite amount of time?
Logarithm of exponential
Fixing obscure 8080 emulator bug?
Is using haveibeenpwned to validate password strength rational?
Why was the Sega Genesis marketed as a 16-bit console?
Should I give professor gift at the beginning of my PhD?
What is the highest possible permanent AC at character creation?
Thread Pool C++ Implementation
How to determine if a hyphen (-) exists inside a column
Alternatives to LIKE and FTS for mid name searchIndexing strategy for dynamic predicateQuery to match exact column or partial data of the column: SQL Server / AzurePerforming search on a column by queryPerform regex search queries of a text column in table of 100+ million using regexDoes there exist a way to determine the exact file that contains an allocation unit in a filegroup of multiple files?How long does it take a full text index to update after inserting a large amount of text?Substring without the first n charactersCan't import flat file into SqlServerSearching for and parsing out specific text parts within the values of a column
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
In a CASE
expression, I'm trying to search inside a text column to identify a hyphen (-):
CASE
WHEN SUBSTRING(al.ALT_ADDRESS,1,1) IN('1','5','7')
AND al.NEW_ADDRESS CONTAINS '-'
THEN CONCAT(al.ALT_ADDRESS,al.NEW_ADDRESS)
The hyphen can be located anywhere in the column, and so I just need to know if it exists, regardless of where it actually is in the column.
I'm currently using the exact same code that @Josh provided (LIKE '%-%'
), but it doesn't work, because it doesn't return the correct data for several specific instances where I know that it "should" be. The exact text in ALT_ADDRESS
is: "2754 Churchill Circle". The exact text in NEW_ADDRESS
is: "O-89421". However, the results that are returned, does not include the NEW_ADDRESS
(O-89421).
I have confirmed that dash in NEW_ADDRESS
really matches the dash I'm using the search (ASCII 45).
sql-server sql-server-2016 string-searching
add a comment |
In a CASE
expression, I'm trying to search inside a text column to identify a hyphen (-):
CASE
WHEN SUBSTRING(al.ALT_ADDRESS,1,1) IN('1','5','7')
AND al.NEW_ADDRESS CONTAINS '-'
THEN CONCAT(al.ALT_ADDRESS,al.NEW_ADDRESS)
The hyphen can be located anywhere in the column, and so I just need to know if it exists, regardless of where it actually is in the column.
I'm currently using the exact same code that @Josh provided (LIKE '%-%'
), but it doesn't work, because it doesn't return the correct data for several specific instances where I know that it "should" be. The exact text in ALT_ADDRESS
is: "2754 Churchill Circle". The exact text in NEW_ADDRESS
is: "O-89421". However, the results that are returned, does not include the NEW_ADDRESS
(O-89421).
I have confirmed that dash in NEW_ADDRESS
really matches the dash I'm using the search (ASCII 45).
sql-server sql-server-2016 string-searching
5
'2754 Churchill Circle
doesn't meet theSUBSTRING(al.ALT_ADDRESS,1,1) IN ('1', '5', '7')
criteria, since the substring is'2'
.
– Barmar
May 22 at 15:49
add a comment |
In a CASE
expression, I'm trying to search inside a text column to identify a hyphen (-):
CASE
WHEN SUBSTRING(al.ALT_ADDRESS,1,1) IN('1','5','7')
AND al.NEW_ADDRESS CONTAINS '-'
THEN CONCAT(al.ALT_ADDRESS,al.NEW_ADDRESS)
The hyphen can be located anywhere in the column, and so I just need to know if it exists, regardless of where it actually is in the column.
I'm currently using the exact same code that @Josh provided (LIKE '%-%'
), but it doesn't work, because it doesn't return the correct data for several specific instances where I know that it "should" be. The exact text in ALT_ADDRESS
is: "2754 Churchill Circle". The exact text in NEW_ADDRESS
is: "O-89421". However, the results that are returned, does not include the NEW_ADDRESS
(O-89421).
I have confirmed that dash in NEW_ADDRESS
really matches the dash I'm using the search (ASCII 45).
sql-server sql-server-2016 string-searching
In a CASE
expression, I'm trying to search inside a text column to identify a hyphen (-):
CASE
WHEN SUBSTRING(al.ALT_ADDRESS,1,1) IN('1','5','7')
AND al.NEW_ADDRESS CONTAINS '-'
THEN CONCAT(al.ALT_ADDRESS,al.NEW_ADDRESS)
The hyphen can be located anywhere in the column, and so I just need to know if it exists, regardless of where it actually is in the column.
I'm currently using the exact same code that @Josh provided (LIKE '%-%'
), but it doesn't work, because it doesn't return the correct data for several specific instances where I know that it "should" be. The exact text in ALT_ADDRESS
is: "2754 Churchill Circle". The exact text in NEW_ADDRESS
is: "O-89421". However, the results that are returned, does not include the NEW_ADDRESS
(O-89421).
I have confirmed that dash in NEW_ADDRESS
really matches the dash I'm using the search (ASCII 45).
sql-server sql-server-2016 string-searching
sql-server sql-server-2016 string-searching
edited May 22 at 14:15
Michael
1134
1134
asked May 21 at 17:39
Mike JonesMike Jones
957
957
5
'2754 Churchill Circle
doesn't meet theSUBSTRING(al.ALT_ADDRESS,1,1) IN ('1', '5', '7')
criteria, since the substring is'2'
.
– Barmar
May 22 at 15:49
add a comment |
5
'2754 Churchill Circle
doesn't meet theSUBSTRING(al.ALT_ADDRESS,1,1) IN ('1', '5', '7')
criteria, since the substring is'2'
.
– Barmar
May 22 at 15:49
5
5
'2754 Churchill Circle
doesn't meet the SUBSTRING(al.ALT_ADDRESS,1,1) IN ('1', '5', '7')
criteria, since the substring is '2'
.– Barmar
May 22 at 15:49
'2754 Churchill Circle
doesn't meet the SUBSTRING(al.ALT_ADDRESS,1,1) IN ('1', '5', '7')
criteria, since the substring is '2'
.– Barmar
May 22 at 15:49
add a comment |
3 Answers
3
active
oldest
votes
An alternative approach to the existing answer is to use the CHARINDEX() function which returns the position of the specified string if exists, otherwise 0.
select charindex('-','kevin-')
Will return 6, because the hyphen is located in the sixth position of the string, compared with
select charindex('-','kevin')
returns 0 because the '-' is not present in the string.
Per John Eisbrener's suggestion, PATINDEX
is also an option, and it allows for wildcards - which may prove beneficial in certain circumstances.
add a comment |
You didn't mention why the code you provided doesn't work. CONTAINS
is for use with SQL Server's full text search feature. If you're not using this, then you need to use a LIKE
clause with wildcards:
CASE WHEN SUBSTRING(al.ALT_ADDRESS,1,1) IN('1','5','7') AND al.NEW_ADDRESS LIKE '%-%'
THEN CONCAT(al.ALT_ADDRESS,al.NEW_ADDRESS)
Even if you are using full text search, the matching behavior with dashes can be unexpected and Microsoft recommends using LIKE
instead.
You might double check that the character is really a hyphen, something like this should work:
SELECT
ASCII('-') as RealHypen,
ASCII(SUBSTRING(NEW_ADDRESS, 1, 1))
FROM YourTable
WHERE ALT_ADDRESS = '2754 Churchill Circle';
As an aside, the LIKE
expression above is not able to make use of the seeking abilities of a b-tree index in SQL Server, so performance may suffer on large tables if a large scanning operation is required. The best way to mitigate that (should it apply to you or others in a similar situation) depends heavily on context, but in general the main alternatives are:
- Indexing a computed column that evaluates the search condition
- Using triggers to persist the search result in advance
- Use an external tool that's more suited to searching text (Elasticsearch is popular)
- Use n-grams (typically for substrings of 3 characters or more)
- Use Full Text Search (this won't help with wildcards per se, or this specific case, but can work for word-based searching).
If you are interested in exploring the performance aspect, please ask a follow up question.
add a comment |
LIKE '%-%'
I'm currently using the exact same code that @Josh provided (
LIKE '%-%'
), but it doesn't work, because it doesn't return the correct data for several specific instances where I know that it "should" be. The exact text inALT_ADDRESS
is: "2754 Churchill Circle". The exact text inNEW_ADDRESS
is: "O-89421". However, the results that are returned, does not include the NEW_ADDRESS (O-89421).
I don't get this problem at all,
SELECT new_address
FROM ( VALUES
('O-89421')
) AS t(new_address)
WHERE new_address LIKE '%-%';
Seems to work for me
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f238714%2fhow-to-determine-if-a-hyphen-exists-inside-a-column%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
An alternative approach to the existing answer is to use the CHARINDEX() function which returns the position of the specified string if exists, otherwise 0.
select charindex('-','kevin-')
Will return 6, because the hyphen is located in the sixth position of the string, compared with
select charindex('-','kevin')
returns 0 because the '-' is not present in the string.
Per John Eisbrener's suggestion, PATINDEX
is also an option, and it allows for wildcards - which may prove beneficial in certain circumstances.
add a comment |
An alternative approach to the existing answer is to use the CHARINDEX() function which returns the position of the specified string if exists, otherwise 0.
select charindex('-','kevin-')
Will return 6, because the hyphen is located in the sixth position of the string, compared with
select charindex('-','kevin')
returns 0 because the '-' is not present in the string.
Per John Eisbrener's suggestion, PATINDEX
is also an option, and it allows for wildcards - which may prove beneficial in certain circumstances.
add a comment |
An alternative approach to the existing answer is to use the CHARINDEX() function which returns the position of the specified string if exists, otherwise 0.
select charindex('-','kevin-')
Will return 6, because the hyphen is located in the sixth position of the string, compared with
select charindex('-','kevin')
returns 0 because the '-' is not present in the string.
Per John Eisbrener's suggestion, PATINDEX
is also an option, and it allows for wildcards - which may prove beneficial in certain circumstances.
An alternative approach to the existing answer is to use the CHARINDEX() function which returns the position of the specified string if exists, otherwise 0.
select charindex('-','kevin-')
Will return 6, because the hyphen is located in the sixth position of the string, compared with
select charindex('-','kevin')
returns 0 because the '-' is not present in the string.
Per John Eisbrener's suggestion, PATINDEX
is also an option, and it allows for wildcards - which may prove beneficial in certain circumstances.
edited May 22 at 12:01
Josh Darnell
9,43032346
9,43032346
answered May 21 at 18:07
kevinnwhatkevinnwhat
69018
69018
add a comment |
add a comment |
You didn't mention why the code you provided doesn't work. CONTAINS
is for use with SQL Server's full text search feature. If you're not using this, then you need to use a LIKE
clause with wildcards:
CASE WHEN SUBSTRING(al.ALT_ADDRESS,1,1) IN('1','5','7') AND al.NEW_ADDRESS LIKE '%-%'
THEN CONCAT(al.ALT_ADDRESS,al.NEW_ADDRESS)
Even if you are using full text search, the matching behavior with dashes can be unexpected and Microsoft recommends using LIKE
instead.
You might double check that the character is really a hyphen, something like this should work:
SELECT
ASCII('-') as RealHypen,
ASCII(SUBSTRING(NEW_ADDRESS, 1, 1))
FROM YourTable
WHERE ALT_ADDRESS = '2754 Churchill Circle';
As an aside, the LIKE
expression above is not able to make use of the seeking abilities of a b-tree index in SQL Server, so performance may suffer on large tables if a large scanning operation is required. The best way to mitigate that (should it apply to you or others in a similar situation) depends heavily on context, but in general the main alternatives are:
- Indexing a computed column that evaluates the search condition
- Using triggers to persist the search result in advance
- Use an external tool that's more suited to searching text (Elasticsearch is popular)
- Use n-grams (typically for substrings of 3 characters or more)
- Use Full Text Search (this won't help with wildcards per se, or this specific case, but can work for word-based searching).
If you are interested in exploring the performance aspect, please ask a follow up question.
add a comment |
You didn't mention why the code you provided doesn't work. CONTAINS
is for use with SQL Server's full text search feature. If you're not using this, then you need to use a LIKE
clause with wildcards:
CASE WHEN SUBSTRING(al.ALT_ADDRESS,1,1) IN('1','5','7') AND al.NEW_ADDRESS LIKE '%-%'
THEN CONCAT(al.ALT_ADDRESS,al.NEW_ADDRESS)
Even if you are using full text search, the matching behavior with dashes can be unexpected and Microsoft recommends using LIKE
instead.
You might double check that the character is really a hyphen, something like this should work:
SELECT
ASCII('-') as RealHypen,
ASCII(SUBSTRING(NEW_ADDRESS, 1, 1))
FROM YourTable
WHERE ALT_ADDRESS = '2754 Churchill Circle';
As an aside, the LIKE
expression above is not able to make use of the seeking abilities of a b-tree index in SQL Server, so performance may suffer on large tables if a large scanning operation is required. The best way to mitigate that (should it apply to you or others in a similar situation) depends heavily on context, but in general the main alternatives are:
- Indexing a computed column that evaluates the search condition
- Using triggers to persist the search result in advance
- Use an external tool that's more suited to searching text (Elasticsearch is popular)
- Use n-grams (typically for substrings of 3 characters or more)
- Use Full Text Search (this won't help with wildcards per se, or this specific case, but can work for word-based searching).
If you are interested in exploring the performance aspect, please ask a follow up question.
add a comment |
You didn't mention why the code you provided doesn't work. CONTAINS
is for use with SQL Server's full text search feature. If you're not using this, then you need to use a LIKE
clause with wildcards:
CASE WHEN SUBSTRING(al.ALT_ADDRESS,1,1) IN('1','5','7') AND al.NEW_ADDRESS LIKE '%-%'
THEN CONCAT(al.ALT_ADDRESS,al.NEW_ADDRESS)
Even if you are using full text search, the matching behavior with dashes can be unexpected and Microsoft recommends using LIKE
instead.
You might double check that the character is really a hyphen, something like this should work:
SELECT
ASCII('-') as RealHypen,
ASCII(SUBSTRING(NEW_ADDRESS, 1, 1))
FROM YourTable
WHERE ALT_ADDRESS = '2754 Churchill Circle';
As an aside, the LIKE
expression above is not able to make use of the seeking abilities of a b-tree index in SQL Server, so performance may suffer on large tables if a large scanning operation is required. The best way to mitigate that (should it apply to you or others in a similar situation) depends heavily on context, but in general the main alternatives are:
- Indexing a computed column that evaluates the search condition
- Using triggers to persist the search result in advance
- Use an external tool that's more suited to searching text (Elasticsearch is popular)
- Use n-grams (typically for substrings of 3 characters or more)
- Use Full Text Search (this won't help with wildcards per se, or this specific case, but can work for word-based searching).
If you are interested in exploring the performance aspect, please ask a follow up question.
You didn't mention why the code you provided doesn't work. CONTAINS
is for use with SQL Server's full text search feature. If you're not using this, then you need to use a LIKE
clause with wildcards:
CASE WHEN SUBSTRING(al.ALT_ADDRESS,1,1) IN('1','5','7') AND al.NEW_ADDRESS LIKE '%-%'
THEN CONCAT(al.ALT_ADDRESS,al.NEW_ADDRESS)
Even if you are using full text search, the matching behavior with dashes can be unexpected and Microsoft recommends using LIKE
instead.
You might double check that the character is really a hyphen, something like this should work:
SELECT
ASCII('-') as RealHypen,
ASCII(SUBSTRING(NEW_ADDRESS, 1, 1))
FROM YourTable
WHERE ALT_ADDRESS = '2754 Churchill Circle';
As an aside, the LIKE
expression above is not able to make use of the seeking abilities of a b-tree index in SQL Server, so performance may suffer on large tables if a large scanning operation is required. The best way to mitigate that (should it apply to you or others in a similar situation) depends heavily on context, but in general the main alternatives are:
- Indexing a computed column that evaluates the search condition
- Using triggers to persist the search result in advance
- Use an external tool that's more suited to searching text (Elasticsearch is popular)
- Use n-grams (typically for substrings of 3 characters or more)
- Use Full Text Search (this won't help with wildcards per se, or this specific case, but can work for word-based searching).
If you are interested in exploring the performance aspect, please ask a follow up question.
edited May 22 at 17:12
Paul White♦
55.6k14293465
55.6k14293465
answered May 21 at 18:00
Josh DarnellJosh Darnell
9,43032346
9,43032346
add a comment |
add a comment |
LIKE '%-%'
I'm currently using the exact same code that @Josh provided (
LIKE '%-%'
), but it doesn't work, because it doesn't return the correct data for several specific instances where I know that it "should" be. The exact text inALT_ADDRESS
is: "2754 Churchill Circle". The exact text inNEW_ADDRESS
is: "O-89421". However, the results that are returned, does not include the NEW_ADDRESS (O-89421).
I don't get this problem at all,
SELECT new_address
FROM ( VALUES
('O-89421')
) AS t(new_address)
WHERE new_address LIKE '%-%';
Seems to work for me
add a comment |
LIKE '%-%'
I'm currently using the exact same code that @Josh provided (
LIKE '%-%'
), but it doesn't work, because it doesn't return the correct data for several specific instances where I know that it "should" be. The exact text inALT_ADDRESS
is: "2754 Churchill Circle". The exact text inNEW_ADDRESS
is: "O-89421". However, the results that are returned, does not include the NEW_ADDRESS (O-89421).
I don't get this problem at all,
SELECT new_address
FROM ( VALUES
('O-89421')
) AS t(new_address)
WHERE new_address LIKE '%-%';
Seems to work for me
add a comment |
LIKE '%-%'
I'm currently using the exact same code that @Josh provided (
LIKE '%-%'
), but it doesn't work, because it doesn't return the correct data for several specific instances where I know that it "should" be. The exact text inALT_ADDRESS
is: "2754 Churchill Circle". The exact text inNEW_ADDRESS
is: "O-89421". However, the results that are returned, does not include the NEW_ADDRESS (O-89421).
I don't get this problem at all,
SELECT new_address
FROM ( VALUES
('O-89421')
) AS t(new_address)
WHERE new_address LIKE '%-%';
Seems to work for me
LIKE '%-%'
I'm currently using the exact same code that @Josh provided (
LIKE '%-%'
), but it doesn't work, because it doesn't return the correct data for several specific instances where I know that it "should" be. The exact text inALT_ADDRESS
is: "2754 Churchill Circle". The exact text inNEW_ADDRESS
is: "O-89421". However, the results that are returned, does not include the NEW_ADDRESS (O-89421).
I don't get this problem at all,
SELECT new_address
FROM ( VALUES
('O-89421')
) AS t(new_address)
WHERE new_address LIKE '%-%';
Seems to work for me
answered May 22 at 14:57
Evan CarrollEvan Carroll
34.1k1184245
34.1k1184245
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- 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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f238714%2fhow-to-determine-if-a-hyphen-exists-inside-a-column%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
5
'2754 Churchill Circle
doesn't meet theSUBSTRING(al.ALT_ADDRESS,1,1) IN ('1', '5', '7')
criteria, since the substring is'2'
.– Barmar
May 22 at 15:49