简体   繁体   English

SQL Server Rex在通配符中获取字符串

[英]SQL server Rex fetch string in wildcard

For example, I have got a string with table name and the schema like: 例如,我有一个带有表名和模式的字符串:

[dbo].[statistical]

How can fetch just the table name statistical out from this string? 如何仅从该字符串中提取statistical的表名?

This is what PARSENAME is used for: 这是PARSENAME的用途:

SELECT PARSENAME('[dbo].[statistical]', 1)
SELECT PARSENAME('[adventureworks].[dbo].[statistical]', 1)
SELECT PARSENAME('[adventureworks]..[statistical]', 1)
SELECT PARSENAME('[statistical]', 1)
SELECT PARSENAME('dbo.statistical', 1)
-- all examples return 'statistical'

You could alternatively try this: 您也可以尝试以下方法:

declare @s varchar(100) = 'asd.stadfa';

select reverse(substring(s, 1, charindex('.', s) - 1)) from (
    select reverse(@s) s
) a

charindex returns first occurence of character, so you reverse initial string to make last dot first. charindex返回字符的第一个出现位置,因此您反转初始字符串以使最后一个点优先。 Then you just use substring to extract first part of reversed string, which is what you are looking for. 然后,您只需要使用子字符串来提取反向字符串的第一部分,这就是您想要的。 Finally, you need to apply reverse one more time to reverse back extracted string :) 最后,你需要申请reverse一个更多的时间来扭转反萃取串:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM