代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
MSSQL
】
SQL Server 中从完整路径提取文件名
作者:
/ 发布于
2018/1/24
/
1000
--1、根据路径获取文件名 -- ============================================= -- Author: Paul Griffin -- Create date: 18 January 2015 -- Description: Returns a filename with extension -- from a full path: -- D:\Temp\Resources\Images\My.Picture.jpg -- ==> My.Picture.jpg -- ============================================= CREATE FUNCTION [dbo].[GetFileName] ( @Path NVARCHAR(MAX) ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @FileName NVARCHAR(MAX) DECLARE @ReversedPath NVARCHAR(MAX) SET @ReversedPath = REVERSE(@Path) SELECT @FileName = RIGHT(@Path, CHARINDEX('\', @ReversedPath)-1) RETURN @FileName END --2、根据路径获取文件名(去掉扩展名) -- ============================================= -- Author: Paul Griffin -- Create date: 18 January 2015 -- Description: Returns a filename without extension -- from a full path: -- D:\Temp\Resources\Images\My.Picture.jpg -- ==> My.Picture -- ============================================= CREATE FUNCTION [dbo].[GetFileNameWithoutExtension] ( @Path NVARCHAR(MAX) ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @FileName NVARCHAR(MAX) DECLARE @ReversedPath NVARCHAR(MAX) DECLARE @ExtLength INT SET @ReversedPath = REVERSE(@Path) SELECT @ExtLength = CHARINDEX('.', @ReversedPath) SELECT @FileName = RIGHT(@Path, CHARINDEX('\', @ReversedPath)-1) SELECT @FileName = LEFT(@FileName, LEN(@FileName) - @ExtLength) RETURN @FileName END 3、根据路径获取路径目录(无文件名) [sql] view plain copy -- ============================================= -- Author: Paul Griffin -- Create date: 18 January 2015 -- Description: Returns the path without the file name -- from a full path: -- D:\Temp\Resources\Images\My.Picture.jpg -- ==> D:\Temp\Resources\Images -- ============================================= CREATE FUNCTION [dbo].[GetDirectoryPath] ( @Path NVARCHAR(MAX) ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @FileName NVARCHAR(MAX) DECLARE @ReversedPath NVARCHAR(MAX) DECLARE @PathLength INT SET @ReversedPath = REVERSE(@Path) SELECT @PathLength = CHARINDEX('\', @ReversedPath) SELECT @FileName = LEFT(@Path, LEN(@Path) - @PathLength) RETURN @FileName END 4、获取文件扩展名 [sql] view plain copy -- ============================================= -- Author: Paul Griffin -- Create date: 18 January 2015 -- Description: Returns the extension only -- from a full path: -- D:\Temp\Resources\Images\My.Picture.jpg -- ==> jpg -- ============================================= CREATE FUNCTION [dbo].[GetExtension] ( @Path NVARCHAR(MAX) ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @FileName NVARCHAR(MAX) DECLARE @ReversedPath NVARCHAR(MAX) DECLARE @ExtLength INT SET @ReversedPath = REVERSE(@Path) SET @FileName = '' SELECT @ExtLength = CHARINDEX('.', @ReversedPath) IF (@ExtLength > 0) BEGIN SELECT @FileName = RIGHT(@Path, @ExtLength - 1) END RETURN @FileName END --代码使用示例1 [sql] view plain copy SELECT Location ,dbo.GetDirectoryPath(Location) AS [Path Only] ,dbo.GetFileName(Location) AS [Full File Name] ,dbo.GetFileNameWithoutExtension(Location) AS [File Name without Ext ,dbo.GetExtension(Location) AS Ext FROM Images --代码使用示例2 [sql] view plain copy declare @filePath nvarchar(4000) set @filepath='D:\messages\attachrecv\0.abe3b3d58161ccb3de7ab63754bb3f15\abc.doc' SELECT dbo.GetDirectoryPath(@filepath) AS [Path Only] ,dbo.GetFileName(@filepath) AS [Full File Name] ,dbo.GetFileNameWithoutExtension(@filepath) AS [File Name without Ext] ,dbo.GetExtension(@filepath) AS Ext 得到结果: path only:D:\messages\attachrecv\0.abe3b3d58161ccb3de7ab63754bb3f15 Full File Name:abc.doc File Name without Ext:abc Ext:.doc 3、根据路径获取路径目录(无文件名) -- ============================================= -- Author: Paul Griffin -- Create date: 18 January 2015 -- Description: Returns the path without the file name -- from a full path: -- D:\Temp\Resources\Images\My.Picture.jpg -- ==> D:\Temp\Resources\Images -- ============================================= CREATE FUNCTION [dbo].[GetDirectoryPath] ( @Path NVARCHAR(MAX) ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @FileName NVARCHAR(MAX) DECLARE @ReversedPath NVARCHAR(MAX) DECLARE @PathLength INT SET @ReversedPath = REVERSE(@Path) SELECT @PathLength = CHARINDEX('\', @ReversedPath) SELECT @FileName = LEFT(@Path, LEN(@Path) - @PathLength) RETURN @FileName END 4、获取文件扩展名 -- ============================================= -- Author: Paul Griffin -- Create date: 18 January 2015 -- Description: Returns the extension only -- from a full path: -- D:\Temp\Resources\Images\My.Picture.jpg -- ==> jpg -- ============================================= CREATE FUNCTION [dbo].[GetExtension] ( @Path NVARCHAR(MAX) ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @FileName NVARCHAR(MAX) DECLARE @ReversedPath NVARCHAR(MAX) DECLARE @ExtLength INT SET @ReversedPath = REVERSE(@Path) SET @FileName = '' SELECT @ExtLength = CHARINDEX('.', @ReversedPath) IF (@ExtLength > 0) BEGIN SELECT @FileName = RIGHT(@Path, @ExtLength - 1) END RETURN @FileName END --代码使用示例1 SELECT Location ,dbo.GetDirectoryPath(Location) AS [Path Only] ,dbo.GetFileName(Location) AS [Full File Name] ,dbo.GetFileNameWithoutExtension(Location) AS [File Name without Ext ,dbo.GetExtension(Location) AS Ext FROM Images --代码使用示例2 declare @filePath nvarchar(4000) set @filepath='D:\messages\attachrecv\0.abe3b3d58161ccb3de7ab63754bb3f15\abc.doc' SELECT dbo.GetDirectoryPath(@filepath) AS [Path Only] ,dbo.GetFileName(@filepath) AS [Full File Name] ,dbo.GetFileNameWithoutExtension(@filepath) AS [File Name without Ext] ,dbo.GetExtension(@filepath) AS Ext 得到结果: path only:D:\messages\attachrecv\0.abe3b3d58161ccb3de7ab63754bb3f15 Full File Name:abc.doc File Name without Ext:abc Ext:.doc
试试其它关键字
同语言下
.
SQL查询 多列合并成一行用逗号隔开
.
查看存储过程修改时间,最近执行时间
.
设置手动批量删除数据库相关进程
.
获取某个表中特定字段的所有字符串形式
.
SQL 如何去除重复的字符串
.
怎么去掉一个字段中的重复数据
.
String 去除空格 回车 换行 水平制表符
.
SQL查询和替换含有回车,空格,TAB
.
SQL SERVER 查询每日新增用户数量、次留数量
.
判断两个字符串是否存在相同的内容
可能有用的
.
SQL查询 多列合并成一行用逗号隔开
.
查看存储过程修改时间,最近执行时间
.
设置手动批量删除数据库相关进程
.
获取某个表中特定字段的所有字符串形式
.
SQL 如何去除重复的字符串
.
怎么去掉一个字段中的重复数据
.
String 去除空格 回车 换行 水平制表符
.
SQL查询和替换含有回车,空格,TAB
.
SQL SERVER 查询每日新增用户数量、次留数量
.
判断两个字符串是否存在相同的内容
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3