SQL Server中的子字符串 [英] Substring in SQL Server

查看:90
本文介绍了SQL Server中的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从sql server中的字符串中获取子字符串

E:\ VORDERINGSSTAAT \ 1_DDP12101PME.pdf

上面的路径存储在sql表中,我想使用sql查询从上面的路径中提取1_DDP12101PME.pdf



任何人都可以帮助plz

I want to fetch a substring from a string in sql server

E:\VORDERINGSSTAAT\1_DDP12101PME.pdf

the above path is stored in sql table, i want to fetch 1_DDP12101PME.pdf using sql query from above path



Anyone help plz

推荐答案

DECLARE @file varchar(255)
DECLARE @charPosition int

SET @file = 'E:\VORDERINGSSTAAT\1_DDP12101PME.pdf'
SET @charPosition = (Len(@file)) - CharIndex('\', Reverse(@file))

SELECT SUBSTRING(@file, @charPosition+2, (LEN(@file) - @charPosition)+1)



返回:1_DDP12101PME.pdf

相同的代码,但是在一个查询中:



returns: 1_DDP12101PME.pdf

Same code, but in one query:

SELECT SUBSTRING(@file, ((Len(@file)) - CharIndex('\', Reverse(@file)))+2, (LEN(@file) - ((Len(@file)) - CharIndex('\', Reverse(@file))))+1)


SELECT Reverse(SUBSTRING(REVERSE(FilePath),0,CHARINDEX('\', REVERSE(FilePath),0)))
 
FROM Files


hey kishore,
use split() function.

like this:
string path= "E:\VORDERINGSSTAAT\1_DDP12101PME.pdf";
string [] pdffilename = path.split('\');
now "1_DDP12101PME.pdf" this value comes into string array.

now get your value like this:
string filename = pdffilename[pdffilename.Length-1];
in filename variable your value are coming... try this in your application hope it will work for you...
all the best.....


这篇关于SQL Server中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆