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

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived