Sql server 2005 在t-sql中搜索模式
我有一个db列,其值为100.23.24.1、100.23.24.2、100.23.24.3等。我必须找到最后一个点后的最后一个数字。在这种情况下,我想要3个。然后我想增加最后一个数字,并生成新的条目100.23.24.4。我怎样才能做到这一点。有人能帮忙吗 我找不到100.23.24。模式,因为这可能会在另一个数据库列中更改,如:100.23.25.1100.23.25.2。在这种情况下,最后一个数字应该是2Sql server 2005 在t-sql中搜索模式,sql-server-2005,tsql,Sql Server 2005,Tsql,我有一个db列,其值为100.23.24.1、100.23.24.2、100.23.24.3等。我必须找到最后一个点后的最后一个数字。在这种情况下,我想要3个。然后我想增加最后一个数字,并生成新的条目100.23.24.4。我怎样才能做到这一点。有人能帮忙吗 我找不到100.23.24。模式,因为这可能会在另一个数据库列中更改,如:100.23.25.1100.23.25.2。在这种情况下,最后一个数字应该是2 使用反转字符索引查找最后一个点 馈入子字符串并再次反转此位以获得最后一个数字 保留保
- 也可以使用嵌套的CHARINDEX+1查找第三个点,而不使用反向
- 在客户端或CLR代码中执行此操作,该代码具有方便的string.split功能
- 也可以使用嵌套的CHARINDEX+1查找第三个点,而不使用反向
- 在客户端或CLR代码中执行此操作,该代码具有方便的string.split功能
或存储。。。你得到这个想法的最佳方式:
UPDATE YourTable
SET YourColumn=LEFT(YourColumn,LEN(YourColumn)-2)+'.4'
WHERE LEFT(REVERSE(YourColumn),2)='3.'
创建4个包含极小或较小int值的新列,然后从字符串列填充它们。在这一点上,放下字符串列。您现在可以轻松地执行以下操作:
UPDATE YourTable Set col4=col4+1 WHERE col4=3
如果愿意,可以添加一个计算列将4个值连接回一个字符串,以支持旧代码
--set up existing table
create table YourTable (YourColumn varchar(20))
INSERT YourTable VALUES ('100.23.24.1')
INSERT YourTable VALUES ('100.23.24.2')
INSERT YourTable VALUES ('100.23.24.3')
INSERT YourTable VALUES ('100.23.24.9')
--add in new columns
ALTER TABLE YourTable ADD Col1 int null
ALTER TABLE YourTable ADD Col2 int null
ALTER TABLE YourTable ADD Col3 int null
ALTER TABLE YourTable ADD Col4 int null
--populate the new columns, split apart the string
UPDATE YourTable
SET Col1=PARSENAME(YourColumn,4)
,Col2=PARSENAME(YourColumn,3)
,Col3=PARSENAME(YourColumn,2)
,Col4=PARSENAME(YourColumn,1)
--remove the string column
ALTER TABLE YourTable drop column YourColumn
--add back the string column as a computed column
ALTER TABLE dbo.YourTable ADD YourColumn AS CONVERT(varchar(10),Col1)+'.'+CONVERT(varchar(10),Col2)+'.'+CONVERT(varchar(10),Col3)+'.'+CONVERT(varchar(10),Col4)
--show the table's contents
select * from YourTable
输出:
Col1 Col2 Col3 Col4 YourColumn
----------- ----------- ----------- ----------- -------------
100 23 24 1 100.23.24.1
100 23 24 2 100.23.24.2
100 23 24 3 100.23.24.3
100 23 24 9 100.23.24.9
(4 row(s) affected)
YourColumn
--------------------
100.23.24.1
100.23.24.2
100.23.24.4
100.23.24.9
除此之外,这里还有一种快速暴力表格扫描方式:
UPDATE YourTable
SET YourColumn=LEFT(YourColumn,LEN(YourColumn)-2)+'.4'
WHERE LEFT(REVERSE(YourColumn),2)='3.'
示例代码:
declare @YourTable table (YourColumn varchar(20))
INSERT @YourTable VALUES ('100.23.24.1')
INSERT @YourTable VALUES ('100.23.24.2')
INSERT @YourTable VALUES ('100.23.24.3')
INSERT @YourTable VALUES ('100.23.24.9')
UPDATE @YourTable
SET YourColumn=LEFT(YourColumn,LEN(YourColumn)-2)+'.4'
WHERE LEFT(REVERSE(YourColumn),2)='3.'
select * from @YourTable
输出:
Col1 Col2 Col3 Col4 YourColumn
----------- ----------- ----------- ----------- -------------
100 23 24 1 100.23.24.1
100 23 24 2 100.23.24.2
100 23 24 3 100.23.24.3
100 23 24 9 100.23.24.9
(4 row(s) affected)
YourColumn
--------------------
100.23.24.1
100.23.24.2
100.23.24.4
100.23.24.9
最佳方式:
UPDATE YourTable
SET YourColumn=LEFT(YourColumn,LEN(YourColumn)-2)+'.4'
WHERE LEFT(REVERSE(YourColumn),2)='3.'
创建4个包含极小或较小int值的新列,然后从字符串列填充它们。在这一点上,放下字符串列。您现在可以轻松地执行以下操作:
UPDATE YourTable Set col4=col4+1 WHERE col4=3
如果愿意,可以添加一个计算列将4个值连接回一个字符串,以支持旧代码
--set up existing table
create table YourTable (YourColumn varchar(20))
INSERT YourTable VALUES ('100.23.24.1')
INSERT YourTable VALUES ('100.23.24.2')
INSERT YourTable VALUES ('100.23.24.3')
INSERT YourTable VALUES ('100.23.24.9')
--add in new columns
ALTER TABLE YourTable ADD Col1 int null
ALTER TABLE YourTable ADD Col2 int null
ALTER TABLE YourTable ADD Col3 int null
ALTER TABLE YourTable ADD Col4 int null
--populate the new columns, split apart the string
UPDATE YourTable
SET Col1=PARSENAME(YourColumn,4)
,Col2=PARSENAME(YourColumn,3)
,Col3=PARSENAME(YourColumn,2)
,Col4=PARSENAME(YourColumn,1)
--remove the string column
ALTER TABLE YourTable drop column YourColumn
--add back the string column as a computed column
ALTER TABLE dbo.YourTable ADD YourColumn AS CONVERT(varchar(10),Col1)+'.'+CONVERT(varchar(10),Col2)+'.'+CONVERT(varchar(10),Col3)+'.'+CONVERT(varchar(10),Col4)
--show the table's contents
select * from YourTable
输出:
Col1 Col2 Col3 Col4 YourColumn
----------- ----------- ----------- ----------- -------------
100 23 24 1 100.23.24.1
100 23 24 2 100.23.24.2
100 23 24 3 100.23.24.3
100 23 24 9 100.23.24.9
(4 row(s) affected)
YourColumn
--------------------
100.23.24.1
100.23.24.2
100.23.24.4
100.23.24.9
除此之外,这里还有一种快速暴力表格扫描方式:
UPDATE YourTable
SET YourColumn=LEFT(YourColumn,LEN(YourColumn)-2)+'.4'
WHERE LEFT(REVERSE(YourColumn),2)='3.'
示例代码:
declare @YourTable table (YourColumn varchar(20))
INSERT @YourTable VALUES ('100.23.24.1')
INSERT @YourTable VALUES ('100.23.24.2')
INSERT @YourTable VALUES ('100.23.24.3')
INSERT @YourTable VALUES ('100.23.24.9')
UPDATE @YourTable
SET YourColumn=LEFT(YourColumn,LEN(YourColumn)-2)+'.4'
WHERE LEFT(REVERSE(YourColumn),2)='3.'
select * from @YourTable
输出:
Col1 Col2 Col3 Col4 YourColumn
----------- ----------- ----------- ----------- -------------
100 23 24 1 100.23.24.1
100 23 24 2 100.23.24.2
100 23 24 3 100.23.24.3
100 23 24 9 100.23.24.9
(4 row(s) affected)
YourColumn
--------------------
100.23.24.1
100.23.24.2
100.23.24.4
100.23.24.9
对于SQL Server,下面的查询显示了如何提取左侧和右侧部分以构建新值:
DECLARE @ip varchar(20)
SET @ip = '100.23.24.1'
SELECT left(@ip, len(@ip) - charindex('.', reverse(@ip))) + '.' +
cast(right(@ip, charindex('.', reverse(@ip)) - 1) + 1 as varchar(3))
在本例中,它将返回100.23.24.2。对于SQL Server,下面的查询显示了如何提取左右部分以构建新值:
DECLARE @ip varchar(20)
SET @ip = '100.23.24.1'
SELECT left(@ip, len(@ip) - charindex('.', reverse(@ip))) + '.' +
cast(right(@ip, charindex('.', reverse(@ip)) - 1) + 1 as varchar(3))
WITH ips AS
(
SELECT '100.23.24.123' AS ip UNION ALL
SELECT '100.23.24.1' AS ip
)
SELECT LEFT(ip, PATINDEX('%' + PARSENAME(ip,1),ip) -1) +
CAST(CAST(PARSENAME(ip,1) AS INT)+1 AS VARCHAR)
FROM ips
在这种情况下,它将返回100.23.24.2。是否始终为3点4节?是否始终为3点4节?非常好,您的解决方案是通用且简单的。这对我有用。谢谢。太好了,你的解决方案是通用的和简单的。这对我有用。非常感谢。
WITH ips AS
(
SELECT '100.23.24.123' AS ip UNION ALL
SELECT '100.23.24.1' AS ip
)
SELECT LEFT(ip, PATINDEX('%' + PARSENAME(ip,1),ip) -1) +
CAST(CAST(PARSENAME(ip,1) AS INT)+1 AS VARCHAR)
FROM ips