Wildcard expression in SQL Server The Next CEO of Stack OverflowHow 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 word?LEFT 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 isn't the Mueller report being released completely and unredacted?

Unclear about dynamic binding

Grabbing quick drinks

What flight has the highest ratio of time difference to flight time?

Can we say or write : "No, it'sn't"?

A small doubt about the dominated convergence theorem

Do I need to write [sic] when a number is less than 10 but isn't written out?

Running a General Election and the European Elections together

How to invert MapIndexed on a ragged structure? How to construct a tree from rules?

Does soap repel water?

Reference request: Grassmannian and Plucker coordinates in type B, C, D

INSERT to a table from a database to other (same SQL Server) using Dynamic SQL

Is French Guiana a (hard) EU border?

Are police here, aren't itthey?

Why is my new battery behaving weirdly?

Why the difference in type-inference over the as-pattern in two similar function definitions?

How did people program for Consoles with multiple CPUs?

How to check if all elements of 1 list are in the *same quantity* and in any order, in the list2?

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?

Why is quantifier elimination desirable for a given theory?

What was the first Unix version to run on a microcomputer?

Why do airplanes bank sharply to the right after air-to-air refueling?

What happened in Rome, when the western empire "fell"?

Is there a difference between "Fahrstuhl" and "Aufzug"



Wildcard expression in SQL Server



The Next CEO of Stack OverflowHow 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 word?LEFT 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










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

583k13011241270




583k13011241270










asked Mar 8 at 15:47









PugalPugal

40214




40214












  • 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

18116




18116












  • 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

82k1070110




82k1070110












  • 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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page