码迷,mamicode.com
首页 > 数据库 > 详细

在 SQL Server 中从完整路径提取文件名(sql 玩转文件路径)

时间:2018-11-06 01:04:01      阅读:641      评论:0      收藏:0      [点我收藏+]

标签:without   with   www   server   func   ESS   sel   des   col   

四个函数:

--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、根据路径获取路径目录(无文件名)

-- =============================================
-- 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

 

 

转载出处:http://www.codeforge.cn/article/249284

 

在 SQL Server 中从完整路径提取文件名(sql 玩转文件路径)

标签:without   with   www   server   func   ESS   sel   des   col   

原文地址:https://www.cnblogs.com/railgunman/p/9912237.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!