简体   繁体   中英

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?

This is what PARSENAME is used for:

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. 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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