Search code examples
sql-servert-sql

How to truncate string using SQL server


i have large string in SQL Server. I want to truncate that string to 10 or 15 character

Original string

this is test string. this is test string. this is test string. this is test string.

Desired string

this is test string. this is ......

Solution

  • If you only want to return a few characters of your long string, you can use:

    select 
      left(col, 15) + '...' col
    from yourtable
    

    See SQL Fiddle with Demo.

    This will return the first 15 characters of the string and then concatenates the ... to the end of it.

    If you want to to make sure than strings less than 15 do not get the ... then you can use:

    select 
      case 
        when len(col)>15
        then left(col, 15) + '...' 
        else col end col
    from yourtable
    

    See SQL Fiddle with Demo