[英]How do I get the just the number from a SQL table where it is stored like this <PersonNumber>013870</PersonNumber>
我有一个表,用于存储诸如ABC这样的信息,我只想在两者之间插入文本。
simple demo 简单的演示
declare @test table
(
xmlElement varchar(1000)
)
insert into @test
values ('<PersonNumber>013870</PersonNumber>')
-- select numbers from xml in the given format
select
cast(SUBSTRING(xmlElement, 15 , CHARINDEX('</PersonNumber>',xmlElement)-15) as int) -- 15 is position where number starts, because 14 is legth of <PersonNumber>
from @test
-- result is 13870
In Oracle there is * REGEXP_SUBSTR * you can use it if you are oracle to get the number you want ans this is an example for you: 在Oracle中,有* REGEXP_SUBSTR *,如果您是oracle,则可以使用它来获取所需的数字,这是您的一个示例:
SELECT
REGEXP_SUBSTR('500 Oracle Parkway, Redwood Shores, CA',
',[^,]+,') "REGEXPR_SUBSTR"
FROM DUAL;
REGEXPR_SUBSTR
-----------------
, Redwood Shores,
If PersonNumber is fixed length you can simply use substring function. 如果PersonNumber是固定长度,则可以简单地使用子字符串函数。
Otherwise use 否则使用
select Substring(PersonNumber, Patindex('%>%',PersonNumber) + 1, Patindex('%< /%',PersonNumber) - Patindex('%>%',PersonNumber)-1) 选择Substring(PersonNumber,Patindex('%>%',PersonNumber)+ 1,Patindex('%</%',PersonNumber)-Patindex('%>%',PersonNumber)-1)
Try this as Sample: 尝试作为示例:
DECLARE @z VARCHAR(32) = ',ukasd10,';
SELECT REPLACE(SUBSTRING(@z, CHARINDEX(',', @z), LEN(@z)), ',', '') AS Sample
and use this logic wherever you want...thanks! 并在任何需要的地方使用此逻辑...谢谢!
With SQL Server, you can use an XML method if the column contains well-formed XML: 对于SQL Server,如果该列包含格式正确的XML,则可以使用XML方法:
SELECT CAST(YourColumn AS xml).value('/Request[1]/Schedule[1]/Employees[1]/PersonIdentity[1]/PersonNumber[1]', 'int') AS PersonNumber
FROM dbo.YourTable;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.