Results 1 to 11 of 11

Thread: Substring

  1. #1
    Join Date
    Jan 2004
    Posts
    164

    Unanswered: Substring

    I have a field that contaings 15 characters. I want to just pull the first 6. So anything that matches these 6 will be returned.

  2. #2
    Join Date
    Jul 2003
    Location
    The Dark Planet
    Posts
    1,401
    select left(colname,6) from tablename
    Get yourself a copy of the The Holy Book

    order has no physical Brett in The meaning of a Kaiser . -database data

  3. #3
    Join Date
    Jan 2004
    Location
    Boston
    Posts
    58
    select substring (cloumn, 1, 6) from tablename

  4. #4
    Join Date
    Jul 2003
    Location
    The Dark Planet
    Posts
    1,401
    Originally posted by rding
    select substring (cloumn, 1, 6) from tablename
    Just wondering ... which would be faster ... substring or left ... not that it matters much anyway ...
    Get yourself a copy of the The Holy Book

    order has no physical Brett in The meaning of a Kaiser . -database data

  5. #5
    Join Date
    Jan 2004
    Location
    Boston
    Posts
    58
    ...um I really think they are identical performance wise.

  6. #6
    Join Date
    Jan 2004
    Posts
    164

    substring

    Thanks guys. That substring statement did the trick beautifilly. Thanks once again.

    estefex

  7. #7
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    select cast(column as char(6))

  8. #8
    Join Date
    Jul 2003
    Location
    The Dark Planet
    Posts
    1,401
    well how many ways can this statement be written as

    we have already seen three ....

    this should also work
    Code:
    select convert(varchar(6),colname) from tablename
    any other ways
    Get yourself a copy of the The Holy Book

    order has no physical Brett in The meaning of a Kaiser . -database data

  9. #9
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    There probably isn't a noticable difference performance-wise, but I'd bet that LEFT is more efficient than SUBSTRING, simply because SUBSTRING is more powerful and more complex. The LEFT logic is pretty simple, and simplest is usually fastest.

    I don't know how CAST would compare, but it probably uses similiar logic to LEFT.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  10. #10
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    Originally posted by Enigma
    well how many ways can this statement be written as

    we have already seen three ....

    any other ways
    Well let's see...

    There's always:

    SELECT REVERSE(REVERSE(LEFT(colname, 2) + SUBSTRING(colname, 3, 4)))

  11. #11
    Join Date
    Feb 2004
    Posts
    7

    LEFT quicker

    LEFT is quicker because it will just grab the appropriate string, whereas SUBSTRING actually has to look through the string.

Posting Permissions

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