I have string:
*rg*niza*io*
I want to replace % in the first and last character of the string. Desired out put is:
%rg*niza*io%
I'm just answering because the obvious to me is:
select '%' + substring(str, 2, len(str) - 2) + '%'
Of course, this would be a bit more complicated if you want to conditionally replace the characters when they are '*'.
If you want to replace only * with % from first and last positions. Then,
Query
SELECT CASE
WHEN LEFT([string_column], 1) = '*' AND RIGHT([string_column], 1) = '*'
THEN '%' + SUBSTRING([string_column], 2, LEN([string_column]) - 2) + '%'
WHEN LEFT([string_column], 1) = '*' AND RIGHT([string_column], 1) <> '*'
THEN '%' + RIGHT([string_column], LEN([string_column]) - 1)
WHEN LEFT([string_column], 1) <> '*' AND RIGHT([string_column], 1) = '*'
THEN LEFT([string_column], LEN([string_column]) - 1) + '%'
ELSE [string_column] END AS [updated_string_column]
FROM [your_table_name];
Demo
Use STUFF,LEFT and LEN string functions
Declare #string varchar(50) = '*rg*niza*io*'
select stuff(left(#string,len(#string)-1),1,1,'%')+'%'
Result : %rg*niza*io%
Use a combination of the CONCAT, LEFT, SUBSTRING & LEFT functions.
SELECT CONCAT('%',LEFT(SUBSTRING(yourfield,2,LEN(yourfield)),LEN(yourfield)-2),'%')
FROM yourtable
Output: %rg*niza*io%
Related
So I am extracting data from one table to another
SELECT *
LTRIM(ADRESSE + ',' + ADRESSE2) AS ADDRESS12
FROM [Homestore].[dbo].[CLIENT]
Issue is that if the cells are blank i still get a comma ,
I have tried using & instead of + but nvarchar is incompatible in the '&' operator. Any ideas how I only insert the comma if there is something to concatenate?
You want the equivalent of CONCAT_WS() in other databases. You can do this with STUFF() and some string logic in SQL Server:
SELECT c.*
STUFF( (COALESCE(',' + ADRESSE, '') +
COALESCE(',' + ADRESSE2, '') +
), 1, 1, ''
) AS ADDRESS12
FROM [Homestore].[dbo].[CLIENT] c;
This structure is convenient, because you can just add more COALESCE() expressions for more columns.
use case expression
SELECT *, LTRIM(ADRESSE + case when ADRESSE is not null then ',' end + ADRESSE2) AS ADDRESS12
FROM [Homestore].[dbo].[CLIENT]
use case when for null checking
SELECT *
LTRIM(ADRESSE + case when ADRESSE2 is not null then
',' else '' end + ADRESSE2) AS ADDRESS12
FROM [Homestore].[dbo].[CLIENT]
I have a table filled with items which have IDs of characters.
Example:
"TGHZN"
"FVGHR"
"OLTVD"
"EERFV" -- invalid because of EE (sequence of the same character)
"EFEDC"
"DFFFB" -- invalid because of FFF
How can I select all rows with invalid IDs?
A invalid ID is defined by a sequence of the equal characters (e.g. AA) within the ID.
I've tried it with RegEx without success: SELECT * FROM Items WHERE ID LIKE '%(.)\1+%'
Another option (no limit on the length of ID)
Select Distinct A.ID
From YourTable A
Join (Select Top 26 Patt=Replicate(Char(64+Row_Number() Over (Order By Number)),2) From master..spt_values ) B
on CharIndex(B.Patt,A.ID)>0
This is not easy to do with SQL Server because SQL Server does not support regular expressions (calling the mild enhancements to 'LIKE' "regular expressions" is serious marketing-overspeak).
Assuming your ids have a length of 5, you can try something like this:
where (id like '%' + substring(id, 1, 1) + substring(id, 1, 1) + '%') or
(id like '%' + substring(id, 2, 1) + substring(id, 2, 1) + '%') or
(id like '%' + substring(id, 3, 1) + substring(id, 3, 1) + '%') or
(id like '%' + substring(id, 4, 1) + substring(id, 4, 1) + '%')
SQL Server does not (out-of-the-box) support regular expressions.
Actually, the replicate() function would make the logic a tad clearer:
where (id like '%' + replicate(substring(id, 1, 1), 2) + '%') or
(id like '%' + replicate(substring(id, 2, 1), 2) + '%') or
(id like '%' + replicate(substring(id, 3, 1), 2) + '%') or
(id like '%' + replicate(substring(id, 4, 1), 2) + '%')
Remove the '+' from the expression you have written. following should work:
SELECT * FROM Items WHERE ID LIKE '%(.)\1%'
I have a query where I need to search the numerical part of a string in SQL Server.
In the number column above needs to be searchable as a variable in the query.
Wildcards does not work:
SELECT PK_Story
FROM Story
WHERE ProductId = #productParam
AND Number LIKE '%' + #numberParam + '%';
because this would also return 132 and 232 for example.
So how can I search for a specific number after the '-'. As you can see I can't do charindex because of the variable prefix length.
What about LIKE '%-' + #numberParam?
You can use substring and charindex combination to get the result.
SELECT PK_Story
FROM Story
WHERE ProductId = #productParam
AND #numberParam like
'%' + case when charindex('-', Number) > 0
then substring(Number, charindex('-', Number) +1, len(Number)) + '%'
else Number
end + '%'
what about this
declare #My_Number as varchar(50)='8'
SELECT PK_Story
FROM Story
WHERE ProductId = #productParam
AND substring(Number, charindex('-', Number) +1, len(Number)) like
#My_Number +'%'
Or, if want equal
SELECT PK_Story
FROM Story
WHERE ProductId = #productParam
AND substring(Number, charindex('-', Number) +1, len(Number)) =
#My_Numbe
I was wondering if it was possible to only substring if the string length is > 2?
Here is my sample statement:
Select SUBSTRING(ABRESC, 1, 2) + '-' + SUBSTRING(ABRESC, 3, 5) AS ABRESC From TABLE
However, some fields are only 2 chars long so i was wondering if its possible to only substring when its longer than 2 chars?
You could use CASE
Select ABRESC =
CASE WHEN LEN(ABRESC) > 2
THEN SUBSTRING(ABRESC, 1, 2) + '-' + SUBSTRING(ABRESC, 3, 5)
ELSE ABRESC END
From TABLE
I want to concatenate a string
I want output like this:
NEX-SYM-VIM-CRE
If the input is NEX-NULL-NULL-VRE, it comes out to be NEX---CRE or ---CRE or NEX--- as i have replaced NULL with -
But concatenation to get a final result like NEX-SYM is not coming
Something like this?
ISNULL(NEX,'-') + '-' + ISNULL(SYM,'-') + '-' + ISNULL(VIM,'-') + '-' + ISNULL(CRE,'-')
Always add a - delimiting character to the right but only when there is a value:
NULLIF(
COALESCE(NEX + '-', '')
+ COALESCE(SYM + '-', '')
+ COALESCE(VIM + '-', '')
+ COALESCE(CRE + '-', ''), ''
)
then you always need to trim the last character (will be a - delimiting character) unless the result is NULL.