Last active
November 8, 2017 16:57
-
-
Save jarrettmeyer/a648a879eb6d29a5a46cd9fc709f850e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
fn_FilenameFromFullPath.sql (c) 2017 Jarrett Meyer | |
Description: | |
Extracts a filename from a full path. Files may have spaces. | |
Examples: | |
dbo.fn_FilenameFromFullPath('file.txt') -> 'file.txt' | |
dbo.fn_FilenameFromFullPath('C:\file.txt') -> 'file.txt' | |
dbo.fn_FilenameFromFullPath('C:\temp\examples\file.txt') -> 'file.txt' | |
*/ | |
IF OBJECT_ID('dbo.fn_FilenameFromFullPath') IS NULL | |
EXEC ('CREATE FUNCTION dbo.fn_FilenameFromFullPath () RETURNS INT AS BEGIN RETURN 0; END'); | |
GO | |
ALTER FUNCTION [dbo].[fn_FilenameFromFullPath] | |
( | |
@fullpath NVARCHAR(256) | |
) | |
RETURNS NVARCHAR(256) | |
AS | |
BEGIN | |
DECLARE @dirSep NVARCHAR(1) = '\'; | |
DECLARE @filename NVARCHAR(256); | |
IF CHARINDEX(@dirSep, @fullpath) > 0 | |
SET @filename = RIGHT(@fullpath, CHARINDEX(@dirSep, REVERSE(@fullpath)) - 1); | |
ELSE | |
SET @filename = @fullpath; | |
RETURN @filename; | |
END | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment