Selecting values from SQL 2000

  • 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.

  • Look up charindex in Books Online

  • 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

  • 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.

  • 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

  • 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?

  • 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

  • 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