English 中文(简体)
选择字符串部分
原标题:Select portion of string

我有一套包含全 UCC 路径到目录的数据集。 我想选择路径( 移动服务器/ IP) 。 我该怎么做?

数据 :

\nas.home.example.comdvdMy First Video
\nas.office.example.comusinessReport1
\nas.office.example.comusinessReport2
\10.10.10.10projectsproj1images
\10.10.10.10projectsproj1queries
\10.10.10.10projectsproj1output

预期结果(可以使用或不使用领先的,我不在乎):

dvdMy First Video
usinessReport1
usinessReport2
projectsproj1images
projectsproj1queries
projectsproj1output

我尝试了以下(具体针对上述投入之一):

SELECT RIGHT(( \nas.home.example.comdvdMy First Video ), 
  CHARINDEX(  , REVERSE( \nas.home.example.comdvdMy First Video )));

这只返回最内部的目录 ( my first Vide ) 。 我可以添加一个偏移来获取父目录, 但只有在目录与字符数完全相同时才有效 :

SELECT RIGHT(( \nas.home.example.comdvdMy First Video ), 
  CHARINDEX(  , REVERSE( \nas.home.example.comdvdMy First Video ))+4);

返回 dvdMy First View ,这对这个值是好的。我如何修改我的查询来为我的所有数据值工作?

我怀疑我可能实际上需要其中两个查询。一个是带有 DNS 名称的服务器,其结尾为 .com ,另一个是IP地址,其开头都是 10.10

最佳回答
DECLARE @x TABLE (p VARCHAR(255))

INSERT @x SELECT  \nas.home.example.comdvdMy First Video 
UNION ALL SELECT  \nas.office.example.comusinessReport1 
UNION ALL SELECT  \nas.office.example.comusinessReport2 
UNION ALL SELECT  \10.10.10.10projectsproj1images 
UNION ALL SELECT  \10.10.10.10projectsproj1queries 
UNION ALL SELECT  \10.10.10.10projectsproj1output 
UNION ALL SELECT  foo.barwhateverwho ;

SELECT p, x = SUBSTRING(p, CHARINDEX(  , SUBSTRING(p, 3, 4000)) + 2, 4000) FROM @x;

结果:

p                                           x
-----------------------------------------   ------------------------
\nas.home.example.comdvdMy First Video   dvdMy First Video
\nas.office.example.comusinessReport1   usinessReport1
\nas.office.example.comusinessReport2   usinessReport2
\10.10.10.10projectsproj1images         projectsproj1images
\10.10.10.10projectsproj1queries        projectsproj1queries
\10.10.10.10projectsproj1output         projectsproj1output
foo.barwhateverwho                        whateverwho

如果您想要删除前导的 , 请将 更改为

问题回答

与亚伦相同的概念, 略有不同的语法

SUBSTRING(p, CHARINDEX(  ,p,3), 255)

尝试此( 重要部分是第三行) :

DECLARE @str varchar(100)
SET @str =  \nas.home.example.comdvdMy First Video 
select substring(@str, CHARINDEX(  , @str, 3), len(@str) - charindex(  , @str, 3))

在您的 >SELECT 语句中以列名替换

这是从第三个字符之后的第一个索引开始抓取字符串的子字符串, 其长度为字符串的总长度减去第三个字符之后的第一个索引。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...