是否有办法以实例取代第1个性案例。
^1402 WSN NIAMLAB^teertS htimS 005
to be
&1402 WSN NIAMLAB^teertS htimS 005
第2条
是否有办法以实例取代第1个性案例。
^1402 WSN NIAMLAB^teertS htimS 005
to be
&1402 WSN NIAMLAB^teertS htimS 005
第2条
为取代第一类特性,建议使用<代码>STUFF和>。 <代码>STUFF
在另一条插图中。 它删除了在起始职位上排出的首处,然后将第二处插入起首位置的第一处。
DECLARE @str varchar(100) = ^1402 WSN NIAMLAB^teertS htimS 005
SELECT STUFF(@str, CHARINDEX( ^ , @str), 1, & )
请注意,您也可在询问中使用<代码>STUFF:
SELECT STUFF(<yourcolumn>, CHARINDEX( ^ , <yourcolumn>), 1, & )
FROM <yourtable>
Although the example in your question is how to replace only the 1st occurance of a char, your title indicates you want to replace the nth occurance which is a bit trickier.
我同时写了一种功能,以发现在扼杀中具有某种特性。 你们只能用来构筑一条带有战略色彩的护栏。
if exists (
select 1
from dbo.sysobjects
where id = object_id(N [dbo].[CHARINDEX3] )
and xtype in (N FN , N IF , N TF ))
begin
drop function [dbo].[CHARINDEX3]
end
GO
/*
Example:
SELECT dbo.CHARINDEX3( a , abb abb a , 3)
SELECT dbo.CHARINDEX3( b , abb abb a , 5)
SELECT dbo.CHARINDEX3( a , abbabba , 3)
SELECT dbo.CHARINDEX3( b , abbabba , 5)
If @occurance > the max Occurrence, the function will return the max Occurrence
*/
CREATE FUNCTION dbo.CHARINDEX3
(
@TargetStr char(1),
@SearchedStr varchar(max),
@Occurrence int
)
RETURNS INT
AS
BEGIN
DECLARE @ret INT
; WITH
-- Tally table Gen Tally Rows: X2 X3
t1 AS (SELECT 1 N UNION ALL SELECT 1 N), -- 4 , 8
t2 AS (SELECT 1 N FROM t1 x, t1 y), -- 16 , 64
t3 AS (SELECT 1 N FROM t2 x, t2 y), -- 256 , 4096
t4 AS (SELECT 1 N FROM t3 x, t3 y), -- 65536 , 16,777,216
t5 AS (SELECT 1 N FROM t4 x, t4 y), -- 4,294,967,296, A lot
Numbers AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) Num
FROM t3 x, t3 y)
SELECT TOP 1 @ret = Num
FROM
(
SELECT Ident = ROW_NUMBER() OVER (ORDER BY N.Num), N.Num
FROM Numbers N
WHERE N.Num <= DATALENGTH(@SearchedStr)
AND SUBSTRING(@SearchedStr, N.Num, DATALENGTH(@TargetStr)) = @TargetStr
) R
WHERE Ident <= @Occurrence
ORDER BY Ident DESC
RETURN @ret
END
GO
Using that function you can get the location of the nth occurance and simply pull all the chars before and after the occurance, replacing the specific value.
--Actual Code
DECLARE @loc INT
DECLARE @MyStr VARCHAR(200)
DECLARE @replacement CHAR(1)
SET @MySTr = ^1402 WSN NIAMLAB^teertS htimS 005
SET @replacement = $
SELECT @loc = dbo.CHARINDEX3( ^ ,@MyStr, 2)
SELECT SUBSTRING(@myStr, 1, @loc-1) + @replacement + SUBSTRING(@MyStr, @loc + 1, LEN(@MyStr)-@loc)
这个例子仅取代第2次<代码>>>>> 。
Untested code:
SELECT STUFF( ^1402 WSN NIAMLAB^teertS htimS 005 , charindex( ^ , ^1402 WSN NIAMLAB^teertS htimS 005 ), 1, & );
GO
然而,如果存在这种特性,你就希望围绕着以下几个方面:charindex (^ , ^1402 WSN NIAMLAB^teert S htimS 005 ! 0
I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...
I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域
We have a restaurant table that has lat-long data for each row. We need to write a query that performs a search to find all restaurants within the provided radius e.g. 1 mile, 5 miles etc. We have ...
What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2
I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...
we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...
I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...
I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...