Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
Hi guys,

I have the following sql query:

C#
"select task_attachment_path \"File Name\" from AttachmentTable";


I got the output in my gridview's cell as:

| File Name |
| ====================================== |
| D:\Live CMS\requestAttachments\23101\Development File.txt |

I want to show only File Name in my Gridview Cell like 'Development File.txt' only.
Can I apply any kind of function in my SQL query or how can I handle it in my C# code.

Please help !!!

Thanks,
AR
Posted
Updated 12-May-14 1:08am
v3

Use the Path.GetFileName[^] method (see the sample code in the documentation page).
 
Share this answer
 
Comments
Maciej Los 18-Dec-13 6:59am    
+5
Please, see my answer ;) I'm using SQL only.
Our solutions complement each other.
CPallini 18-Dec-13 7:11am    
Thank you.
Karthik_Mahalingam 18-Dec-13 7:41am    
good answer
CPallini 18-Dec-13 7:56am    
Thank you.
AR547 18-Dec-13 23:38pm    
(y) Good clue.. :)
 
Share this answer
 
Comments
Maciej Los 18-Dec-13 7:20am    
Please, read question carefully. OP wants to extract the name of file from path. Improve your anwer, OK ;)
Note: i'll not vote till that time.
[no name] 18-Dec-13 7:22am    
k.....Thanks...
You can use SUBSTRING[^] in sql.

e.g

SQL
select substring('select task_attachment_path \"File Name\" from AttachmentTable',31,9)
 
Share this answer
 
Comments
Maciej Los 18-Dec-13 7:01am    
The length of path may be differ...
Do you know what i mean?
;)
[EDIT]
Please, see my answer.
Note: I'm not down-voted your answer
[/EDIT]
[no name] 18-Dec-13 7:32am    
i know it may differ..i just gave as an example..
Maciej Los 18-Dec-13 7:53am    
Up-voted, because of AR547 answer!
AR547 18-Dec-13 23:39pm    
substring, replace, ltrim, rtrim they all can work in sql query to get the specific text/character from a string.
Just learned yesterday :D
Test it:
SQL
DECLARE @ffilename VARCHAR(255)
SET @ffilename = 'D:\Live CMS\requestAttachments\23101\Development File.txt'

SELECT REVERSE(LEFT(REVERSE(@ffilename),CHARINDEX('\',REVERSE(@ffilename))-1)) AS ShortFileName, @ffilename AS FullFileName

Result:
ShortFileName           FullFileName
Development File.txt	D:\Live CMS\requestAttachments\23101\Development File.txt
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 18-Dec-13 7:41am    
excellent +5!
Maciej Los 18-Dec-13 7:52am    
Thank you ;)
AR547 18-Dec-13 13:13pm    
awesome
Maciej Los 18-Dec-13 15:17pm    
Thank you ;)
BK 4 code 19-Dec-13 6:54am    
its not a big deal
C#
"select replace(TASK_ATTACHMENT_PATH,'D:\\Live CMS\\requestAttachments\\23101\\') from TABLE";


The Function of REPLACE worked perfect in this scenario in SQL Qyery.
It gave me only text after \23101\ which I needed to display.

:)

Thanks to all for great work too.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900