in need to update all the rows that have sub string like this:
'forcestartpage=xx'and replace it with 'forcestartpage=18'
* there is lots of characters before and after the substring that shouldnt change
tried this, doesnt work:
update t_reminderscont
set body = REPLACE (body,'forcestartpage=__','forcestartpage=18')
thanks
You can get away with a STUFF function:
SELECT
T.body,
Replaced = STUFF(
T.Body, -- Insert in T.Body
CHARINDEX('forcestartpage=', T.Body), -- ... at the position where 'forcestartpage=' starts
LEN('forcestartpage=18'), -- ... while replacing 17 characters
'forcestartpage=18') -- ... the value forcestartpage=18
FROM
YourTable AS T
WHERE
T.body LIKE '%forcestartpage=__%' AND
T.body NOT LIKE '%forcestartpage=18%'
However this will only work for the first appearance of the forcestartpage= on each row.
Related
I do replacing
SET #data = REPLACE(#data, 'Riched20 10.0.19041}', '');
It is ok, but recently I have detected that it can be
Riched20 10.0.18362 etc
How can I replace in common case like 'Riched20 ...}'?
Can I use a regular-expression?
It should be implementation in T-SQL
If the problem is as simple as this question makes it out to be then you could, instead, find the position of the string 'Riched20' in your value, and then the position of the first } that appears after it and use STUFF to remove the text in that range.
This assumes that the value will always have a } after 'Riched20', and that if there is a } then 'Riched20' also appears. If this isn't the case you will get the value NULL.
DECLARE #data varchar(100) = 'sdjkafhbgtajl asdgasdf, Riched20 10.0.19041} dlkghbsdfl';
SET #data = STUFF(#data, CHARINDEX('Riched20',#data),CHARINDEX('}',#data,CHARINDEX('Riched20',#data)) - CHARINDEX('Riched20',#data) +1,'');
SELECT #data;
If you need "true" pattern replacement, then you are out of luck; T-SQL does not support this as the comments mention.
I have URL data in a table like this(Showing only one row i have many more in table)
abc/portfolio/12/strategy
abc/portfolio/15/strategy
abc/portfolio/1/strategy
The data is in four part separated with '/'.
i got a string to match with this data, either i got a same string to match or if i can get "*" at any part of data if i don't need to match the particular part Like
abc/portfolio/*/strategy
if i get this string to match i don't have to match the '*' part,rest i need to match with the data. i need to match abc/portfolio/% with i need to Match %/strategy in a single column data.
i don't know how to do it with wildcards also as in data its not define that what is the length of a particular part.
As well as if i got two '**' then i need to match only intial Part of Data
Like if i get data to match abc/**/12/strategythen i only need to match abc\% in data.
Example:
Select Url_data from schema1.table1
where
url_data Like 'abc/portfolio/*/strategy'
here i need the "*" to work like, ignore the data in that particular Part.
outcome expected is :
abc/portfolio/12/strategy
abc/portfolio/15/strategy
abc/portfolio/1/strategy
Instead of "*" you can use '%' as follows
Select Url_data from schema1.table1
where
url_data Like 'abc/portfolio/%/strategy'
declare #v varchar(100) = 'abc/portfolio/*/strategy'
Select Url_data from schema1.table1 where url_data Like
replace(
case
when charindex('**',#v) > 0 then left(#v, charindex('**',#v))
else #v
end
,'*','%')
This will replace the * by % if only one * is in the variable #v or truncate the text after ** and replace ** it by %.
declare #v varchar(100) = 'abc/portfolio/*/strategy' -> abc/portfolio/%/strategy
declare #v varchar(100) = 'abc/portfolio/**/strategy' --> abc/portfolio/%
I get an error where it states invalid column 'DatabaseSQL' when I try to add a database reference into an OUTPUT statement like below:
OUTPUT [DatabaseSQL] +'.dbo.Package' 'TableName', 'PackageID', inserted.PackageId,
Core.updXMLFragment('StatusID', inserted.StatusID, Deleted.StatusID)
INTO #OutputList
If I remove the DatabaseSQL and added it as a string like OUTPUT 'Database.dbo.Package..., then it works fine, but I need the statement to actually recognise the database as well as add it as a string, just outputting it as a string alone isn't good enough. Any ideas?
You could use: DB_NAME():
OUTPUT DB_NAME() + '.dbo.Package', 'TableName', 'PackageID' ...
If you store DB name in variable use:
DECLARE #DatabaseSQL SYSNAME = QUOTENAME('name with space');
OUTPUT #DatabaseSQL + '.dbo.Package', 'TableName', 'PackageID' ...
LiveDemo
i am using a stored procedure, where it is taking policy number as parameter which is varchar. I need to eliminate the last 4 characters of the policy number when we retrive from the tables. But the data for policy numbers is not consistent, so I am confused how to use the logic for this. The sample policy numbers are:
KSDRE0021-000
APDRE-10-21-000
KSDRE0021
APDRE-10-21
These are four formats where policies are there in our tables.For some policies there is no tailing end '-000', so that is the challenging part. Now, I need to eliminate the tailing part '-000' from the policies when I retrieve the data from tables.
This is the sample code, which is pulling the policy data from tables.
Create Proc usp.dbo.policydataSP #policy_num varchar(18)
AS
Begin
Select * from policy_table pt
where pt.policy_num = #policy_num
End
STEP 1: Create a User Defined Function to normalize a policy number.
create function dbo.normalize_policy_num
(#policy_num varchar(100))
returns varchar(100)
as
begin
-- replace trailing digits
if (#policy_num like '%-[0-9][0-9][0-9]')
set #policy_num = left(#policy_num, len(#policy_num) - 4)
-- replace remaining hyphens
set #policy_num = replace(#policy_num, '-', '')
return #policy_num
end
What this essentially doing is stripping off the trailing '-000' from policy numbers that contain the pattern, then removing remaining hyphens. This function seems to work on your supplied policy numbers:
-- returns: KSDRE0021
select dbo.normalize_policy_num('KSDRE0021-000')
-- returns: APDRE1021
select dbo.normalize_policy_num('APDRE-10-21-000')
-- returns: KSDRE0021
select dbo.normalize_policy_num('KSDRE0021')
-- returns: APDRE1021
select dbo.normalize_policy_num('APDRE-10-21')
STEP 2: Modify your SP as follows:
create proc usp.dbo.policydataSP
#policy_num varchar(18)
as
begin
select
dbo.normalize_policy_num(pt.policy_num) as normalized_policy_num,
pt.*
from policy_table pt
where dbo.normalize_policy_num(#policy_num) = dbo.normalize_policy_num(pt.policy_num)
Note: If you are able to modify the table schema, you could add a persisted computed column using the UDF specified above. If you add an index to it, queries will run much faster. However, there will be some penalty for inserts, so there is a trade-off.
this is probably your best bet. Match the policy number up to the length of the requested parameter:
Create Proc usp.dbo.policydataSP
#policy_num varchar(18)
AS
Begin
Select * from policy_table pt where LEFT(len(#policy_num),pt.policy_num) = #policy_num
End
If you only want to strip -000 when returning results:
select case right(policy_num, 4)
when '-000' then left(policy_num, len(policy_num) - 4)
else policy_num end as policy_num
from policy_table pt
where pt.policy_num = #policy_num
If you want to strip any 3-digit value following a dash:
select case when policy_num like '%-[0-9][0-9][0-9]' then left(policy_num, len(policy_num) - 4)
else policy_num end as policy_num
from policy_table pt
where pt.policy_num = #policy_num
Can we add null check over one of the parameter of procedure inside WHERE clause and avoid block of code getting repeated like
select aa=t1.aa,bb=t2.bb ,cc=t3.cc
from t1,t2,t3,t4,t5
where t1.p1=t2.p1
and t2.p2=t3.p2
IF(procParameter IS NOT NULL)
and t3.p2=procParameter
and t4.p2=t5.p2
and t5.p3=t1.p2
Look I want one of the AND to get executed conditionally and otherwise it should NOT GET EXECUTED AT ALL..!!!!
How should I go for this optimisation??
I dont want code repeatation like
IF(procParameter IS NOT NULL)
begin
select aa=t1.aa,bb=t2.bb ,cc=t3.cc
from t1,t2,t3,t4,t5
where t1.p1=t2.p1
and t2.p2=t3.p2
and t3.p2=procParameter
and t4.p2=t5.p2
and t5.p3=t1.p2
end
Else
begin
select aa=t1.aa,bb=t2.bb ,cc=t3.cc
from t1,t2,t3,t4,t5
where t1.p1=t2.p1
and t2.p2=t3.p2
and t4.p2=t5.p2
and t5.p3=t1.p2
end
Thanks,
This is one approach:
AND (procParameter IS NULL OR t3.p2=procParameter)