Click here to Skip to main content
Click here to Skip to main content
Go to top

An Easy Way to Get a File Name or a File Extension from a User Defined File Path using Transact-SQL

, 9 Aug 2009
Rate this:
Please Sign up or sign in to vote.
This article will demonstrate how to get a file name or a file extension as well using Transact-SQL.

Introduction

This article will demonstrate how to get a file name or a file extension as well using Transact-SQL.

Background

Several times, developers need to get only the file name or file extension from a file path when they are working on storing the file in binary format into SQL server database, considering the file name and the file extension as a record for further references. Now a days, Microsoft .NET Framework provides some excellent methods for getting the file name or its extension as well. We can consider the scenario where I have an application, it may be windows or web, where I use Visual Basic 6.0 / vbScript and one of the new features will be to store a user defined file in binary format into SQL Server database. So we need to develop a function which will return the file name / extension. In this context, my concern is that it is also possible by using Transact–SQL.

Using the Code

Working with the code is very simple; if you look at the scripts you will see that I used a very common technique which is string reverse logic for doing this. I wrote two stored procedures:

  • dbo.spGetDocumentName
  • dbo.spGetDocumentNature

A sample code example is given below:

Example - 1

-- =============================================
-- Author: Md. Marufuzzaman>
-- Create date:
-- Description: Return the file name from a given location.
-- =============================================
/*
DECLARE @VAL VARCHAR(MAX) 
EXEC dbo.spGetDocumentName 'E:\Guideline & Programming Tools & _
    Resources\Resource - 2\Microsoft Visual Basic 6.0 Source\myFile.pdf', @VAL OUTPUT
PRINT @VAL
*/
CREATE PROCEDURE [dbo].[spGetDocumentName]
 -- Add the parameters for the stored procedure here
  @FILE_PATH   VARCHAR(MAX) 
 ,@NAME        VARCHAR(MAX) OUTPUT
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;
--DECLARE @FILE_PATH VARCHAR(MAX)
DECLARE @FILE_LENGTH INT
DECLARE @FILE_NAME VARCHAR(MAX)
 
 SET @FILE_LENGTH = LEN(@FILE_PATH)
 WHILE (@FILE_LENGTH <> 0)
 BEGIN
  IF SUBSTRING(@FILE_PATH, @FILE_LENGTH, 1) = '\' 
   BEGIN
    SET @FILE_NAME = RIGHT(@FILE_PATH, LEN(@FILE_PATH) - @FILE_LENGTH)
    BREAK
   END
 SET @FILE_LENGTH = @FILE_LENGTH - 1
 END
   
 SET @NAME = @FILE_NAME
 RETURN 
END
GO

Example - 2

-- =============================================
-- Author: Md. Marufuzzaman>
-- Create date: 
-- Description: Return the nature of the file from a given location.
-- =============================================
/*
DECLARE @VAL VARCHAR(MAX) 
EXEC dbo.spGetDocumentNature'E:\Guideline & Programming Tools & _
    Resources\Resource - 2\Microsoft Visual Basic 6.0 Source\myFile.pdf', @VAL OUTPUT
PRINT @VAL
*/
CREATE PROCEDURE [dbo].[spGetDocumentNature]
 -- Add the parameters for the stored procedure here
  @FILE_PATH   VARCHAR(MAX) 
 ,@EXTN       VARCHAR(10) OUTPUT
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;
--DECLARE @FILE_PATH VARCHAR(MAX)
DECLARE @FILE_LENGTH INT
DECLARE @FILE_NATURE VARCHAR(10)
 
 SET @FILE_LENGTH = LEN(@FILE_PATH)
 WHILE (@FILE_LENGTH <> 0)
 BEGIN
  IF SUBSTRING(@FILE_PATH, @FILE_LENGTH, 1) = '.' 
   BEGIN
    SET @FILE_NATURE = RIGHT(@FILE_PATH, LEN(@FILE_PATH) - @FILE_LENGTH)
    BREAK
   END
 SET @FILE_LENGTH = @FILE_LENGTH - 1
 END
    
 SET @EXTN = @FILE_NATURE
 RETURN 
END

Conclusion

I hope that you like it. Enjoy!

History

  • 9th August 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Md. Marufuzzaman
CEO
Bangladesh Bangladesh
A highly experienced leader with successful track record of software development, product innovations, brand management and corporate communication etc. Some successful product innovations have also achieved and awards “Most Valuable Professional” (MVP) at 2010 and 2011 by codeproject.com and also selected as a mentor of codeproject.com. Published over 100 technical articles in various software development resource sites (i.e., codeprojetc.com, Microsoft MSDN, and IEEE & IBM (In progress)) and various IT Forums, Blogs etc.
 
Over ten years of professional experiences in ICT field having extensive experience in formulating corporate vision and long term strategy. Leading development related functions including design, development, services, data management and analytics, customer experience management, content services, digital analytics and optimization.
 
An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint. Believes in transparency, commitment and teamwork.
 
Expertise: Software/Solution Architect, SaaS platform base application, Large scale win32/web based business software solutions, enterprise applications, integration, etc.
 
Technologies/Tools: Microsoft.Net version 05/08/10/12, Microsoft SQL Server version 7/2K/05/08/12 , Oracle version 10/11, MySql version 5.1, 5.5, PS2, Visual C#, R, VB.NET, ASP.NET, PHP, API, MVC, WebAPI , Add-In Visual Basic etc.,. I have also more than two years’ of strong experience in mobile-VAS (platform development).
 
Points of Interest: Technology and research & development especially focused on business functionalities and social business areas as well, few stuff including:
 
1.R&D on new techniques as required to increase business revenue.
2.Urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc., using innovative technical solutions.
3.Research and innovative product development.
4.Etc.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralInteresting Pinmemberspoodygoon9-Aug-09 6:50 
GeneralRe: Interesting PingroupMd. Marufuzzaman9-Aug-09 7:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 9 Aug 2009
Article Copyright 2009 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid