我正在尝试替换其中一个SQL列中的文本。通过查找/*REPLACE*/的所有引用,可以很容易地找到要替换的文本,其中下一个字符是一个数值,该数值必须向上提升5000。
原始值的示例如下所示。
var checkIn = moment(FIELD(/*REPLACE*/4,/*REPLACE*/9).GET());
var checkOut = moment(FIELD(/*REPLACE*/4,/*REPLACE*/10).GET());
if (checkIn > checkOut) {
FIELD(/*REPLACE*/4,/*REPLACE*/10).SET(checkIn.format("DD MMM YYYY"));
checkOut = moment(FIELD(/*REPLACE*/4,/*REPLACE*/10).GET());
}
FIELD(/*REPLACE*/4,/*REPLACE*/11).SET(parseFloat(checkOut.diff(checkIn, "days")).toFixed(2));我需要找到/*REPLACE*/的所有引用,并增加以下字符,这是一个数字5000。
新值的示例如下所示。
var checkIn = moment(FIELD(5004,5009).GET());
var checkOut = moment(FIELD(5004,5010).GET());
if (checkIn > checkOut) {
FIELD(5004,5010).SET(checkIn.format("DD MMM YYYY"));
checkOut = moment(FIELD(5004,5010).GET());
}
FIELD(5004,5011).SET(parseFloat(checkOut.diff(checkIn, "days")).toFixed(2));我已经从基础开始了,但在寻找文本后面的数字字符时迷失了方向。
SELECT column.REPLACE(column, '/*REPLACE*/',有什么需要帮忙的吗?
发布于 2013-07-02 01:46:36
假设您的列是varchar或类似类型(而不是text),则可以使用T-SQL脚本进行替换。
您将需要迭代每个/REPLACE/ string,然后使用另一个嵌套循环来搜索下一个跟随数字。一旦你知道最后一个数字在哪里,你可以将这些数字转换成一个数字,并用该数字+ 5000替换所需的字符串。
我给你贴了一个例子。您可以立即运行此脚本来测试它,但您需要根据您的表/列来调整它。
此外,如果列的类型是Text,也可以执行相同的操作,但需要将文本拆分为varchar块并迭代每个块。如果你需要帮助,请告诉我。
代码如下:
-- table variable, simulating the table to be modified, for testing purposes
-- all references to @tbl and col should be replaced by the intended table/column
declare @tbl table (col varchar(max))
insert into @tbl values ('var checkIn = moment(FIELD(/*REPLACE*/4,/*REPLACE*/9).GET());
var checkOut = moment(FIELD(/*REPLACE*/4,/*REPLACE*/10).GET());
if (checkIn > checkOut) {
FIELD(/*REPLACE*/4,/*REPLACE*/10).SET(checkIn.format("DD MMM YYYY"));
checkOut = moment(FIELD(/*REPLACE*/4,/*REPLACE*/10).GET());
}
FIELD(/*REPLACE*/4,/*REPLACE*/11).SET(parseFloat(checkOut.diff(checkIn, "days")).toFixed(2));')
declare @col_txt varchar(max),
@replace_at int, @digit_start int, @digit_end int, @number int
select @col_txt = col from @tbl -- TODO add a where clause, and consider using a cursor for each row
set @replace_at = 0
-- loop for each /*REPLACE*/ string
while (1=1) begin
set @replace_at = charindex('/*REPLACE*/', @col_txt, @replace_at + 1)
if (@replace_at = 0) break
set @digit_start = @replace_at + len('/*REPLACE*/')
set @digit_end = @digit_start
-- loop while the next char is a number, and save its position
while (ascii(substring(@col_txt, @digit_end, 1)) between ascii(0) and ascii(9)) set @digit_end = @digit_end + 1
set @number = cast(substring(@col_txt, @digit_start, @digit_end - @digit_start) as int) + 5000
-- replace current /*REPLACE*/ and number with the found number + 5000 in the current text chunk
set @col_txt = stuff(@col_txt, @replace_at, @digit_end - @replace_at, @number)
end
-- update the original table with the value
update @tbl set col = @col_txt -- TODO add a where clause
-- only for test purposes
select col from @tbl发布于 2013-06-26 22:18:22
您真的需要在SQL Server上使用T-SQL吗?第三代编程语言(如C++、C#、VB.NET等)支持更强大的字符串操作方法;例如,您可以使用正则表达式,这些正则表达式不是在SQL Server中本机实现的。
您可以设计SQL CLR函数或过程来解决此限制,但更合适的替代方法是使用客户端应用程序。还可以将SQL Server Integration Services与脚本转换结合使用(可以使用C#或VB.NET进行转换)。
毫升
发布于 2013-06-27 01:22:54
您可以创建一个简单的CLR函数来为您执行regex操作。拥有可用于SQL查询的regex函数会非常有帮助。
请看这篇文章:Regular Expressions Make Pattern Matching And Data Extraction Easier
https://stackoverflow.com/questions/17321476
复制相似问题