Wildcard expression in SQL Server Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do I perform an IF…THEN in an SQL SELECT?How to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?Check if table exists in SQL ServerHow to validate an email address using a regular expression?Regular expression to match a line that doesn't contain a wordLEFT JOIN vs. LEFT OUTER JOIN in SQL ServerInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Find all tables containing column with specified name - MS SQL Server

Why doesn't the university give past final exams' answers?

false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'

What is the evidence that custom checks in Northern Ireland are going to result in violence?

Putting Ant-Man on house arrest

`FindRoot [ ]`::jsing: Encountered a singular Jacobian at a point...WHY

Is it OK if I do not take the receipt in Germany?

What was Apollo 13's "Little Jolt" after MECO?

The 'gros' functor from schemes into (strictly) locally ringed topoi

How to compute a Jacobian using polar coordinates?

Did war bonds have better investment alternatives during WWII?

A journey... into the MIND

When I export an AI 300x60 art board it saves with bigger dimensions

Can gravitational waves pass through a black hole?

How did Elite on the NES work?

/bin/ls sorts differently than just ls

How would it unbalance gameplay to rule that Weapon Master allows for picking a fighting style?

Why I cannot instantiate a class whose constructor is private in a friend class?

How to translate "red flag" into Spanish?

Suing a Police Officer Instead of the Police Department

How can I wire a 9-position switch so that each position turns on one more LED than the one before?

All ASCII characters with a given bit count

Co-worker works way more than he should

Does a Draconic Bloodline sorcerer's doubled proficiency bonus for Charisma checks against dragons apply to all dragon types or only the chosen one?

When speaking, how do you change your mind mid-sentence?



Wildcard expression in SQL Server



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do I perform an IF…THEN in an SQL SELECT?How to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?Check if table exists in SQL ServerHow to validate an email address using a regular expression?Regular expression to match a line that doesn't contain a wordLEFT JOIN vs. LEFT OUTER JOIN in SQL ServerInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Find all tables containing column with specified name - MS SQL Server



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I know that, the following query returns the rows, which are contain the exact 5 characters between the A and G



select * 
from
(select 'prefixABBBBBGsuffix' code /*this will be returned. */
union
select 'prefixABBBBGsuffix') rex
where
code like '%A_____G%'


But I want 17 character between A and G, then like condition must have 17 underscores. So I search little in google I found [] will be used in like. Then I tried so for.



select * 
from
(select 'AprefixABBBBBGsuffixG' code
union
select 'AprefixABBBBGsuffixG') rex
where
code like '%A[_]^17G%' /*As per my understanding, '[]' makes a set. And
'^17' would be power of the set (like Mathematics).*/


Then it returns the NULL set. How can I search rows which has certain number of character in the set []?



Note:



I'm using SQL Server 2012.










share|improve this question
























  • sql server doesn't support full regex.

    – scsimon
    Mar 8 at 20:32











  • in sql, [] can make a set, like [A-Za-z] which matches exactly one alphabet char upper and lower case, but when used with _ it's an escape. example, like '%5[%]' will match any string ending in 5% and [_] means a literal _ and []] means a literal ].

    – JBJ
    Mar 8 at 23:17

















0















I know that, the following query returns the rows, which are contain the exact 5 characters between the A and G



select * 
from
(select 'prefixABBBBBGsuffix' code /*this will be returned. */
union
select 'prefixABBBBGsuffix') rex
where
code like '%A_____G%'


But I want 17 character between A and G, then like condition must have 17 underscores. So I search little in google I found [] will be used in like. Then I tried so for.



select * 
from
(select 'AprefixABBBBBGsuffixG' code
union
select 'AprefixABBBBGsuffixG') rex
where
code like '%A[_]^17G%' /*As per my understanding, '[]' makes a set. And
'^17' would be power of the set (like Mathematics).*/


Then it returns the NULL set. How can I search rows which has certain number of character in the set []?



Note:



I'm using SQL Server 2012.










share|improve this question
























  • sql server doesn't support full regex.

    – scsimon
    Mar 8 at 20:32











  • in sql, [] can make a set, like [A-Za-z] which matches exactly one alphabet char upper and lower case, but when used with _ it's an escape. example, like '%5[%]' will match any string ending in 5% and [_] means a literal _ and []] means a literal ].

    – JBJ
    Mar 8 at 23:17













0












0








0








I know that, the following query returns the rows, which are contain the exact 5 characters between the A and G



select * 
from
(select 'prefixABBBBBGsuffix' code /*this will be returned. */
union
select 'prefixABBBBGsuffix') rex
where
code like '%A_____G%'


But I want 17 character between A and G, then like condition must have 17 underscores. So I search little in google I found [] will be used in like. Then I tried so for.



select * 
from
(select 'AprefixABBBBBGsuffixG' code
union
select 'AprefixABBBBGsuffixG') rex
where
code like '%A[_]^17G%' /*As per my understanding, '[]' makes a set. And
'^17' would be power of the set (like Mathematics).*/


Then it returns the NULL set. How can I search rows which has certain number of character in the set []?



Note:



I'm using SQL Server 2012.










share|improve this question
















I know that, the following query returns the rows, which are contain the exact 5 characters between the A and G



select * 
from
(select 'prefixABBBBBGsuffix' code /*this will be returned. */
union
select 'prefixABBBBGsuffix') rex
where
code like '%A_____G%'


But I want 17 character between A and G, then like condition must have 17 underscores. So I search little in google I found [] will be used in like. Then I tried so for.



select * 
from
(select 'AprefixABBBBBGsuffixG' code
union
select 'AprefixABBBBGsuffixG') rex
where
code like '%A[_]^17G%' /*As per my understanding, '[]' makes a set. And
'^17' would be power of the set (like Mathematics).*/


Then it returns the NULL set. How can I search rows which has certain number of character in the set []?



Note:



I'm using SQL Server 2012.







regex tsql sql-server-2012 wildcard






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 16:44









marc_s

586k13011281273




586k13011281273










asked Mar 8 at 15:47









PugalPugal

402114




402114












  • sql server doesn't support full regex.

    – scsimon
    Mar 8 at 20:32











  • in sql, [] can make a set, like [A-Za-z] which matches exactly one alphabet char upper and lower case, but when used with _ it's an escape. example, like '%5[%]' will match any string ending in 5% and [_] means a literal _ and []] means a literal ].

    – JBJ
    Mar 8 at 23:17

















  • sql server doesn't support full regex.

    – scsimon
    Mar 8 at 20:32











  • in sql, [] can make a set, like [A-Za-z] which matches exactly one alphabet char upper and lower case, but when used with _ it's an escape. example, like '%5[%]' will match any string ending in 5% and [_] means a literal _ and []] means a literal ].

    – JBJ
    Mar 8 at 23:17
















sql server doesn't support full regex.

– scsimon
Mar 8 at 20:32





sql server doesn't support full regex.

– scsimon
Mar 8 at 20:32













in sql, [] can make a set, like [A-Za-z] which matches exactly one alphabet char upper and lower case, but when used with _ it's an escape. example, like '%5[%]' will match any string ending in 5% and [_] means a literal _ and []] means a literal ].

– JBJ
Mar 8 at 23:17





in sql, [] can make a set, like [A-Za-z] which matches exactly one alphabet char upper and lower case, but when used with _ it's an escape. example, like '%5[%]' will match any string ending in 5% and [_] means a literal _ and []] means a literal ].

– JBJ
Mar 8 at 23:17












2 Answers
2






active

oldest

votes


















1














same answer as previously but corrected. 17 wasn't the number, it was 18 and 19 for strings, also put in the len(textbetweenA and G) to show.



select rex.* 
from (
select len('prefixABBBBBGsuffix') leng, 'AprefixABBBBBGsuffixG' code
union
select len('prefixABBBBGsuffix'), 'AprefixABBBBGsuffixG'
union
select 0, 'A___________________G'
) rex
where
rex.code like '%A' + replicate('_',19) + 'G%'

--and with [] the set would be [A-Za-z]. Notice this set does not match the A___________________G string.

select rex.*
from (
select len('prefixABBBBBGsuffix') leng, 'AprefixABBBBBGsuffixG' code
union
select len('prefixABBBBGsuffix'), 'AprefixABBBBGsuffixG'
union
select 0, 'A___________________G'
) rex
where
rex.code like '%A' + replicate('[A-Za-z]',19) + 'G%'


[A-Za-z0-9] matches one character within the scope of alphabet (both cases) or a number 0 through 9



I can't find any working information about another way to handle a number of chars like that, replicate is just a way to ease parameterization and typing.






share|improve this answer

























  • It gives the exact answer, even I was added some data like union select 'A'+REPLICATE ('_',17)+'G'

    – Pugal
    Mar 9 at 7:21












  • But I cant understood, why 19 was passed? I want 17 character. So my where clause as rex.code like '%A' + replicate('[A-Za-z]',17) + 'G%'

    – Pugal
    Mar 9 at 7:28











  • neither of the original examples with A and G on the ends had 17 chars between, one had 18 and one had 19. so I chose one of those numbers to show in the example. remove the extra character(s) and you can get 17 :)

    – JBJ
    Mar 9 at 8:10


















2














I would use REPLICATE to generate desired number of '_':



select * from (
select 'prefixABBBBBGsuffix' code
union
select 'prefixABBBBGsuffix'
) rex
where code like '%A' + REPLICATE('_',17) + 'G%';





share|improve this answer























  • Thanks for response. union select 'A'+REPLICATE ('_',17)+'G' if I add this, it will be returned. So I go with @JBJ answer.

    – Pugal
    Mar 9 at 7:25












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%2f55066618%2fwildcard-expression-in-sql-server%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














same answer as previously but corrected. 17 wasn't the number, it was 18 and 19 for strings, also put in the len(textbetweenA and G) to show.



select rex.* 
from (
select len('prefixABBBBBGsuffix') leng, 'AprefixABBBBBGsuffixG' code
union
select len('prefixABBBBGsuffix'), 'AprefixABBBBGsuffixG'
union
select 0, 'A___________________G'
) rex
where
rex.code like '%A' + replicate('_',19) + 'G%'

--and with [] the set would be [A-Za-z]. Notice this set does not match the A___________________G string.

select rex.*
from (
select len('prefixABBBBBGsuffix') leng, 'AprefixABBBBBGsuffixG' code
union
select len('prefixABBBBGsuffix'), 'AprefixABBBBGsuffixG'
union
select 0, 'A___________________G'
) rex
where
rex.code like '%A' + replicate('[A-Za-z]',19) + 'G%'


[A-Za-z0-9] matches one character within the scope of alphabet (both cases) or a number 0 through 9



I can't find any working information about another way to handle a number of chars like that, replicate is just a way to ease parameterization and typing.






share|improve this answer

























  • It gives the exact answer, even I was added some data like union select 'A'+REPLICATE ('_',17)+'G'

    – Pugal
    Mar 9 at 7:21












  • But I cant understood, why 19 was passed? I want 17 character. So my where clause as rex.code like '%A' + replicate('[A-Za-z]',17) + 'G%'

    – Pugal
    Mar 9 at 7:28











  • neither of the original examples with A and G on the ends had 17 chars between, one had 18 and one had 19. so I chose one of those numbers to show in the example. remove the extra character(s) and you can get 17 :)

    – JBJ
    Mar 9 at 8:10















1














same answer as previously but corrected. 17 wasn't the number, it was 18 and 19 for strings, also put in the len(textbetweenA and G) to show.



select rex.* 
from (
select len('prefixABBBBBGsuffix') leng, 'AprefixABBBBBGsuffixG' code
union
select len('prefixABBBBGsuffix'), 'AprefixABBBBGsuffixG'
union
select 0, 'A___________________G'
) rex
where
rex.code like '%A' + replicate('_',19) + 'G%'

--and with [] the set would be [A-Za-z]. Notice this set does not match the A___________________G string.

select rex.*
from (
select len('prefixABBBBBGsuffix') leng, 'AprefixABBBBBGsuffixG' code
union
select len('prefixABBBBGsuffix'), 'AprefixABBBBGsuffixG'
union
select 0, 'A___________________G'
) rex
where
rex.code like '%A' + replicate('[A-Za-z]',19) + 'G%'


[A-Za-z0-9] matches one character within the scope of alphabet (both cases) or a number 0 through 9



I can't find any working information about another way to handle a number of chars like that, replicate is just a way to ease parameterization and typing.






share|improve this answer

























  • It gives the exact answer, even I was added some data like union select 'A'+REPLICATE ('_',17)+'G'

    – Pugal
    Mar 9 at 7:21












  • But I cant understood, why 19 was passed? I want 17 character. So my where clause as rex.code like '%A' + replicate('[A-Za-z]',17) + 'G%'

    – Pugal
    Mar 9 at 7:28











  • neither of the original examples with A and G on the ends had 17 chars between, one had 18 and one had 19. so I chose one of those numbers to show in the example. remove the extra character(s) and you can get 17 :)

    – JBJ
    Mar 9 at 8:10













1












1








1







same answer as previously but corrected. 17 wasn't the number, it was 18 and 19 for strings, also put in the len(textbetweenA and G) to show.



select rex.* 
from (
select len('prefixABBBBBGsuffix') leng, 'AprefixABBBBBGsuffixG' code
union
select len('prefixABBBBGsuffix'), 'AprefixABBBBGsuffixG'
union
select 0, 'A___________________G'
) rex
where
rex.code like '%A' + replicate('_',19) + 'G%'

--and with [] the set would be [A-Za-z]. Notice this set does not match the A___________________G string.

select rex.*
from (
select len('prefixABBBBBGsuffix') leng, 'AprefixABBBBBGsuffixG' code
union
select len('prefixABBBBGsuffix'), 'AprefixABBBBGsuffixG'
union
select 0, 'A___________________G'
) rex
where
rex.code like '%A' + replicate('[A-Za-z]',19) + 'G%'


[A-Za-z0-9] matches one character within the scope of alphabet (both cases) or a number 0 through 9



I can't find any working information about another way to handle a number of chars like that, replicate is just a way to ease parameterization and typing.






share|improve this answer















same answer as previously but corrected. 17 wasn't the number, it was 18 and 19 for strings, also put in the len(textbetweenA and G) to show.



select rex.* 
from (
select len('prefixABBBBBGsuffix') leng, 'AprefixABBBBBGsuffixG' code
union
select len('prefixABBBBGsuffix'), 'AprefixABBBBGsuffixG'
union
select 0, 'A___________________G'
) rex
where
rex.code like '%A' + replicate('_',19) + 'G%'

--and with [] the set would be [A-Za-z]. Notice this set does not match the A___________________G string.

select rex.*
from (
select len('prefixABBBBBGsuffix') leng, 'AprefixABBBBBGsuffixG' code
union
select len('prefixABBBBGsuffix'), 'AprefixABBBBGsuffixG'
union
select 0, 'A___________________G'
) rex
where
rex.code like '%A' + replicate('[A-Za-z]',19) + 'G%'


[A-Za-z0-9] matches one character within the scope of alphabet (both cases) or a number 0 through 9



I can't find any working information about another way to handle a number of chars like that, replicate is just a way to ease parameterization and typing.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 9 at 4:27

























answered Mar 8 at 23:10









JBJJBJ

32116




32116












  • It gives the exact answer, even I was added some data like union select 'A'+REPLICATE ('_',17)+'G'

    – Pugal
    Mar 9 at 7:21












  • But I cant understood, why 19 was passed? I want 17 character. So my where clause as rex.code like '%A' + replicate('[A-Za-z]',17) + 'G%'

    – Pugal
    Mar 9 at 7:28











  • neither of the original examples with A and G on the ends had 17 chars between, one had 18 and one had 19. so I chose one of those numbers to show in the example. remove the extra character(s) and you can get 17 :)

    – JBJ
    Mar 9 at 8:10

















  • It gives the exact answer, even I was added some data like union select 'A'+REPLICATE ('_',17)+'G'

    – Pugal
    Mar 9 at 7:21












  • But I cant understood, why 19 was passed? I want 17 character. So my where clause as rex.code like '%A' + replicate('[A-Za-z]',17) + 'G%'

    – Pugal
    Mar 9 at 7:28











  • neither of the original examples with A and G on the ends had 17 chars between, one had 18 and one had 19. so I chose one of those numbers to show in the example. remove the extra character(s) and you can get 17 :)

    – JBJ
    Mar 9 at 8:10
















It gives the exact answer, even I was added some data like union select 'A'+REPLICATE ('_',17)+'G'

– Pugal
Mar 9 at 7:21






It gives the exact answer, even I was added some data like union select 'A'+REPLICATE ('_',17)+'G'

– Pugal
Mar 9 at 7:21














But I cant understood, why 19 was passed? I want 17 character. So my where clause as rex.code like '%A' + replicate('[A-Za-z]',17) + 'G%'

– Pugal
Mar 9 at 7:28





But I cant understood, why 19 was passed? I want 17 character. So my where clause as rex.code like '%A' + replicate('[A-Za-z]',17) + 'G%'

– Pugal
Mar 9 at 7:28













neither of the original examples with A and G on the ends had 17 chars between, one had 18 and one had 19. so I chose one of those numbers to show in the example. remove the extra character(s) and you can get 17 :)

– JBJ
Mar 9 at 8:10





neither of the original examples with A and G on the ends had 17 chars between, one had 18 and one had 19. so I chose one of those numbers to show in the example. remove the extra character(s) and you can get 17 :)

– JBJ
Mar 9 at 8:10













2














I would use REPLICATE to generate desired number of '_':



select * from (
select 'prefixABBBBBGsuffix' code
union
select 'prefixABBBBGsuffix'
) rex
where code like '%A' + REPLICATE('_',17) + 'G%';





share|improve this answer























  • Thanks for response. union select 'A'+REPLICATE ('_',17)+'G' if I add this, it will be returned. So I go with @JBJ answer.

    – Pugal
    Mar 9 at 7:25
















2














I would use REPLICATE to generate desired number of '_':



select * from (
select 'prefixABBBBBGsuffix' code
union
select 'prefixABBBBGsuffix'
) rex
where code like '%A' + REPLICATE('_',17) + 'G%';





share|improve this answer























  • Thanks for response. union select 'A'+REPLICATE ('_',17)+'G' if I add this, it will be returned. So I go with @JBJ answer.

    – Pugal
    Mar 9 at 7:25














2












2








2







I would use REPLICATE to generate desired number of '_':



select * from (
select 'prefixABBBBBGsuffix' code
union
select 'prefixABBBBGsuffix'
) rex
where code like '%A' + REPLICATE('_',17) + 'G%';





share|improve this answer













I would use REPLICATE to generate desired number of '_':



select * from (
select 'prefixABBBBBGsuffix' code
union
select 'prefixABBBBGsuffix'
) rex
where code like '%A' + REPLICATE('_',17) + 'G%';






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 15:49









Lukasz SzozdaLukasz Szozda

82.9k1072111




82.9k1072111












  • Thanks for response. union select 'A'+REPLICATE ('_',17)+'G' if I add this, it will be returned. So I go with @JBJ answer.

    – Pugal
    Mar 9 at 7:25


















  • Thanks for response. union select 'A'+REPLICATE ('_',17)+'G' if I add this, it will be returned. So I go with @JBJ answer.

    – Pugal
    Mar 9 at 7:25

















Thanks for response. union select 'A'+REPLICATE ('_',17)+'G' if I add this, it will be returned. So I go with @JBJ answer.

– Pugal
Mar 9 at 7:25






Thanks for response. union select 'A'+REPLICATE ('_',17)+'G' if I add this, it will be returned. So I go with @JBJ answer.

– Pugal
Mar 9 at 7:25


















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%2f55066618%2fwildcard-expression-in-sql-server%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







Popular posts from this blog

AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

Алба-Юлія

Захаров Федір Захарович