【问题标题】:SQL - get the values between the curly brackets from a stringSQL - 从字符串中获取大括号之间的值
【发布时间】:2014-06-05 07:25:36
【问题描述】:

我使用的是 SQL Server 2008。

如何从字符串中获取“{”和“}”之间的值并将其放入临时表中

DECLARE @myString VARCHAR(100) = 'my value {Year}{Month}{Day} sample'
create table #temp(Tag varchar(50))

我需要将“Year”、“Month”、“Day”从字符串@myString 插入到临时表中

这样做有什么逻辑吗?

【问题讨论】:

  • 字符串是否总是相同但日期不同?

标签: sql string sql-server-2008 logic temp-tables


【解决方案1】:

将所有{ 替换为<X>,将所有} 替换为</X>。使用nodes() 转换为 XML 并在交叉应用中粉碎 xml。使用value()提取值。

declare @myString varchar(100) = 'my value {Year}{Month}{Day} sample';

select T2.X.value('.', 'varchar(50)')
from (select cast(replace(replace((select @myString for xml path('')), '{', '<X>'), '}', '</X>') as xml).query('.')) as T1(X)
  cross apply T1.X.nodes('/X/text()') as T2(X);

SQL Fiddle

【讨论】:

  • 非常好的创意方案,没想到用这种方式使用xml-functions
猜你喜欢
  • 2023-03-16
  • 2013-02-04
  • 1970-01-01
  • 2016-03-24
  • 2016-08-30
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 2011-07-17
相关资源
最近更新 更多