Directory Path for tempdb

LauraC 21 Reputation points
2022-09-29T19:29:21.387+00:00

I am trying to find out how to just get the directory of the tempdb. I found this query
SELECT filename FROM tempDB.sys.sysfiles;

When I run this query, I get this in one column
G:\Program Files\Microsoft SQL Server\MSSQL15.DEV01\MSSQL\Data\tempdb.mdf

I just want to get
G:\Program Files\Microsoft SQL Server\MSSQL15.DEV01\MSSQL\Data\

How do I substring this to get just the directory path and not include the file name.

For example, I found this InstanceDefaultBackupPath, to find the directory path for the Instance Default Backup Path. What about something like that for the tempdb default backup path?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,145 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,665 questions
0 comments No comments
{count} votes

Accepted answer
  1. LiHongMSFT-4306 29,111 Reputation points
    2022-09-30T02:08:41.18+00:00

    Hi @LauraC
    Try this query:

    SELECT DISTINCT REVERSE(SUBSTRING(REVERSE(filename),CHARINDEX('\',Reverse(filename))+1,LEN(filename))) AS FilePath  
    FROM tempDB.sys.sysfiles;  
    

    Output:
    246246-image.png

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Tom Phillips 17,741 Reputation points
    2022-09-29T20:17:25.353+00:00
    0 comments No comments

  2. Yitzhak Khabinsky 26,201 Reputation points
    2022-09-30T02:22:18.157+00:00

    Hi @LauraC ,

    Here is the easiest way.

    SQL

    SELECT filename  
    	, directory = REPLACE(filename, 'tempdb.mdf', '')  
    FROM tempDB.sys.sysfiles  
    WHERE name = 'tempdev';  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.