SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Removing CHARS from sql return (SQL Server)

    Hello,

    I want to return a row from a table through sql. Now the row returns this:

    3/1573/ImageName.jpg
    I want to return it like this:

    1573/ImageName.jpg
    I am using SQL Server so i tried something like this:

    PHP Code:
    SUBSTRING(SI.LocationCHARINDEX('/'2), 1)  AS Testing 
    But this returns as blank so it doesn't work. Any ideas how i can get this working?

    SI is the name of the table and Location is the row.

    Thanks
    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    try
    Code:
    SUBSTRING( SI.Location
             , CHARINDEX('/',SI.Location,1)+1
             , LEN(SI.Location) - 
               CHARINDEX('/',SI.Location,1)
             ) AS Testing
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks r937 that worked. I understand the CHARINDEX part where you are finding the index of where to start from.

    Am i right in thinking that the LEN(SI.Location) - CHARINDEX('/', SI.Location, 1) is checking for the slash and removing up to it?

    Thanks again
    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by billy_111 View Post
    Am i right in thinking that the LEN(SI.Location) - CHARINDEX('/', SI.Location, 1) is checking for the slash and removing up to it?
    sort of, in a way... it's actually just a length calculation

    the "removal" is done by the SUBSTRING function
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •