简体   繁体   English

我如何从这样存储的SQL表中获取数字 <PersonNumber> 013870 </PersonNumber>

[英]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.

相关问题 如何做一个SQL从这样的表中获取数据? - How to do a sql to get data from a table like this? C#和SQL Server:如何通过使用C#调用存储过程来从SQL表中获取值? - C# and SQL Server: How do I get values from my SQL Table, by calling my stored procedure with C#? 我如何实现一个表,其中很少有属性只是对另一个元组的引用? - sql - How do I implement a table where few attributes are just references to another tuple? SQL-如何使用其他表格中的数字获取名称? - SQL - How do I get name using a number from a different table? 如何运行 SQL,例如“SELECT * from table where columnA = temp_table_result”? - How can I run the SQL like "SELECT * from table where columnA = temp_table_result"? Oracle:如何获取刚刚插入的行的序列号? - Oracle: How do I get the sequence number of the row just inserted? 如何从PHP SQL中的多个表中获取数据? - How do I get data from multiple table in PHP SQL? 如何从tarantool中选择数量有限的记录,例如在SQL中使用SELECT LIMIT? - How do I select a limited number of records from tarantool, like with SELECT LIMIT in SQL? SQL:如何在SQL中过滤(WHERE)连接(ON)表? - SQL: How do I filter (WHERE) a joined (ON) table in SQL? 在SQL中,如何获取列的值在表中最低的所有行? - In SQL, how do I get all rows where a column's value is the lowest in the table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM