March 5, 2008 at 6:47 am
Hi,
I am not an advanced SQL query writer but have a fairly good idea about querying the DB.
I have a value which is "testfolder/test.aspx" in the PageName column of my table.
When I run a select, the entire path is returned.
How do I select only the file name? In this case "test.aspx" ONLY using query.
Kindly help me figure this out.
Thanks for all your input.
March 5, 2008 at 7:36 am
Look up charindex in Books Online
March 5, 2008 at 7:44 am
select right('testfolder/test.aspx',len('testfolder/test.aspx') - charindex('/','testfolder/test.aspx'))
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
March 5, 2008 at 8:53 am
ok, this works very well. 🙂
SELECT RIGHT(PageName, LEN(PageName) - CHARINDEX('/', PageName)) AS Expr1
BUT the page names returned has the first letter removed....
why is this??
i am not able to figure that out. please help.
thanks again for your valuable inputs.
March 5, 2008 at 9:11 am
Hmmm, maybe non-printing characters.
See if this returns different values than the LEN statement.
SELECT DATALENGTH('testfolder/test.aspx')
That might be the issue.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
March 5, 2008 at 9:26 am
SELECT DATALENGTH('testfolder/test.aspx') returns one character more than the result dataset.
Which means it is returning the correct number of character.
Why am I not able to print it then?
March 5, 2008 at 9:30 am
If it's one more, then you've got extra stuff in there. Try using DATALENGTH in the query instead & see how that works.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
March 5, 2008 at 9:45 am
WOW!!!
That works like a charm.
Thank you so much.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply