T-SQL in SQL Server 2025: Substring Changes

  • Comments posted to this topic are about the item T-SQL in SQL Server 2025: Substring Changes

  • Thanks for posting your issue and hopefully someone will answer soon.

    This is an automated bump to increase visibility of your question.

  • Me parece muy interesante el artículo. Gracias.

    Sin embargo me surge una duda que planteo a continuación:

    Se detalló el ejemplo para la fila 75 donde se encuentra "Art Braunschweiger", pero me preguntó que ocurre con el caso de la fila 80 donde se encuentra "Miguel Angel Paolino". El lastname a obtener debería ser Paolino, pero hacer esta separación no es un caso trivial.

  • racopa, apologies, as I'm not very fluent in Spanish. If Google translated this appropriately, you're wondering how to capture a last name like "Paolino" rather than "Angel Paolino"?

    Tracking names in different cultures is hard, as we don't have hard definitions of what a surname is. In this case, how can you tell that "Miguel Angel Paolino" is three names (first, middle, surname) and not two? (Felipe García Rodríguez) where the parents were Juan García Reyes and Carla Rodríguez García.

    If you wanted to try and only capture the first first name (Miguel) and last surname (Paolino), what I'd likely to is look for the first space and get the initial substring. In this case, use CHARINDEX and find that 8 is the first space after Miguel and take the first 7 characters.

    Then, reverse the string, do the same thing and get 8 charaters to a space, take 7 for "oniloap". Then reverse that result to get paolino.

  • Gracias por la respuesta.

    Aún con la barrera del lenguaje creo que si se comprendió la naturaleza y complejidad de mi pregunta.

    Y sigo teniendo claro que la respuesta está lejos de ser trivial y por tanto no visualizo como obtenerla con una simple sentencia de SQL.

    Nuevamente agradezco.

  • Hopefully this helps. Try this code with some names

    SELECT
    ContactName,
    SUBSTRING(ContactName, 1, CHARINDEX(' ', ContactName)) AS ContactFirstName,
    REVERSE(SUBSTRING(REVERSE(ContactName), 1, CHARINDEX(' ', REVERSE(ContactName)))) AS ContactLastName,
    REVERSE(ContactName) AS ReverseContactName,
    CHARINDEX(' ', REVERSE(ContactName)) AS SpaceinReversedName,
    SUBSTRING(REVERSE(ContactName), 1, CHARINDEX(' ', REVERSE(ContactName))) AS ReversedLastName
    FROM dbo.Customers;

    Results in Northwind

    2025-11_0153

     

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply