Results 1 to 7 of 7

Thread: Order By Lastname

  1. #1

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    Order By Lastname

    I have a field called contactname, which looks like this
    Code:
    Al Davis
    Bob Smith
    Robert Smith
    John Anderson
    Jake Anderson
    What I would like to do is somehow order it by the lastname.
    Anyone know how I can do this, i have been using substring and charindex, etc...but haven't been able to figure out how to order this field by the last name.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    have you been able to return Lastname as a separate column?

    if so, you can order by the column (if you return it), or by the formula you would use to create it (but you may need to Select either the formula or the contactname field).

    if not let us know, and I'll have a look.

  3. #3

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    No, i haven't been able to separate them.
    The database is SQL Server 2000.

    Ideally I would like them to be listed like this
    Code:
    Anderson Jake
    Anderson John
    Davis Al
    Smith Bob
    Smith Robert
    The reason is I need to remove duplicates. So if someone is listed as Bob Smith and Robert Smith. I can spot it fairly quickly.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  4. #4
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    You mentioned using substring and charindex, can you just not get that to work properly or do you want something else?

    Here is a working example of using CharIndex and SubString to order the table by last name (assuming that the first and last name are always seperated by a space):
    Code:
    Select Substring(contact, Charindex(' ', contact)+1, Len(contact) - CharIndex(' ', contact)) +
    	' ' + Substring(contact, 1, Charindex(' ', contact) - 1)
    From myTable
    Order By Substring(contact, Charindex(' ', contact)+1, Len(contact) - CharIndex(' ', contact)) +
    	' ' + Substring(contact, 1, Charindex(' ', contact) - 1)
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  5. #5

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    That's almost identical to how I did it, but I didnt' use substring.
    Code:
    SELECT LTrim(Right(ContactName, CHARINDEX(' ', Reverse(ContactName)))) + ', ' +
    	Left(ContactName, CHARINDEX(' ', Reverse(ContactName))) As 'ContactName'
    FROM ContactList
    Order By 'ContactName'
    However, I get this error when trying to run your sql statement
    Server: Msg 536, Level 16, State 3, Line 1
    Invalid length parameter passed to the substring function.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  6. #6
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    Code:
    DECLARE  @db_data_tablesA TABLE ( table_id int identity(1,1), my_names varchar(25) )
    SET NOCOUNT ON
    insert @db_data_tablesA( my_names ) values ('Al Davis')
    insert @db_data_tablesA( my_names ) values ('Bob Smith')
    insert @db_data_tablesA( my_names ) values ('Robert Smith')
    insert @db_data_tablesA( my_names ) values ('John Anderson')
    insert @db_data_tablesA( my_names ) values ('Jake Anderson')
    insert @db_data_tablesA( my_names ) values ('Randa Styss Anderson')
    insert @db_data_tablesA( my_names ) values ('Randa Styss-Anderson')
    
    SELECT * 
      FROM @db_data_tablesA  
      LEFT JOIN
      ( SELECT shortName 
          FROM
          ( SELECT RIGHT( my_names, CHARINDEX(' ', REVERSE( my_names))- 1 ) 
              FROM @db_data_tablesA  
          ) AS a( shortName ) GROUP BY shortName HAVING COUNT(*) > 1
      ) AS b( shorty ) 
      ON shorty = RIGHT( my_names, CHARINDEX(' ', REVERSE( my_names))- 1 )
    ORDER BY shorty, my_names
    HTH

  7. #7
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524
    Originally posted by Memnoch1207
    That's almost identical to how I did it, but I didnt' use substring.
    Code:
    SELECT LTrim(Right(ContactName, CHARINDEX(' ', Reverse(ContactName)))) + ', ' +
    	Left(ContactName, CHARINDEX(' ', Reverse(ContactName))) As 'ContactName'
    FROM ContactList
    Order By 'ContactName'
    However, I get this error when trying to run your sql statement
    I've tried the T-SQL from vb_dba.... and found no error. Are you sure you copy-paste the same T-SQL ?
    Regards,
    [-w-]

Posting Permissions

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



Click Here to Expand Forum to Full Width