EDIT:
I tried to simplify my question without success by not adding the entire script, after getting unrelated answers to my question I added the entire script.
DDL for CountryTabs Table:
CREATE TABLE [dbo].[CountryTabs](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CountryID] [int] NULL,
[LanguageType] [smallint] NOT NULL,
[Name] [nvarchar](200) NOT NULL,
[Content] [nvarchar](max) NULL,
[CategoryOrder] [smallint] NOT NULL,
[Status] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
--find out how many occurnces
DECLARE @OccurnceTbl table(ID int, Content nvarchar(max),Occurnce int)
INSERT into @OccurnceTbl
SELECT ID,Content, LEN(Content) - LEN(REPLACE(Content, 'http://www.example.com/country_page.asp?countryID=','http://www.example.com/country_page.asp?countryID')) AS count
FROM CountryTabs where ID = 71
DECLARE @Occurnce int;
SELECT @Occurnce = Occurnce From @OccurnceTbl
DECLARE @Counter int = 0;
WHILE (@Counter < @Occurnce)
Begin
DECLARE @ID int;
DECLARE @Content nvarchar(max);
DECLARE @NewContent nvarchar(max);
SELECT @ID = ID from @OccurnceTbl
SELECT @Content = Content from CountryTabs where ID = @ID
DECLARE @startIndex int = PATINDEX(N'%http://www.example.com/country_page.asp?countryID%',@Content);
DECLARE @contentWithoutStart NVARCHAR(max) = SUBSTRING(@Content, @startIndex, LEN(@Content))
DECLARE @endIndex int = PATINDEX(N'%">%', @contentWithoutStart) + @startIndex-1;
DECLARE @lengthToReplace int = @endIndex - @startIndex;
DECLARE @strToReplace nvarchar(500) = SUBSTRING(@Content , @startIndex , @lengthToReplace);
SELECT @NewContent = REPLACE(@Content, @strToReplace ,'https://example-new.com')
UPDATE CountryTabs set Content = @NewContent where ID = @ID
SET @Counter = @Counter + 1;
--Select @startIndex,@endIndex,@lengthToReplace,@Counter,@strToReplace
End
Result is many question marks???????? except for the places where
https://example-new.com
Content Column:
Content nvarchar no -1 yes (n/a) (n/a) SQL_Latin1_General_CP1_CI_AS
The Content
column in the table CountryTabs
is of type NVARCHAR(MAX)
and it already contains chars properly stored without question marks, only after the update they appear.
What am I missing here?
I'm keen to know if this works as expected:
DECLARE @Content nvarchar(max);
SELECT @Content = Content from CountryTabs where ID = @ID
DECLARE @NewContent nvarchar(max);
DECLARE @strToReplace nvarchar(500) = N'http://www.example.com';
SELECT @NewContent = REPLACE(@Content, @strToReplace ,N'https://new-example.com')
UPDATE CountryTabs set Content = @NewContent where ID = @ID
SELECT Content from CountryTabs where id = @ID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.