Need help on Substring

  • Hi,

    I've table and data as following,

    declare @t1 table (Id int identity(1,1), first_name varchar(200));

    insert into @t1(first_name) values('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
    insert into @t1(first_name) values('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB');
    insert into @t1(first_name) values('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB');
    insert into @t1(first_name) values('CCCCCC');
    insert into @t1(first_name) values('DDDD');

    select SUBSTRING(first_name, 1, 10) as first_name from @t1

    How to display like below,

    first_name
    --------------
    AAAAAAAAAA...
    BBBBBBBBBB...
    BBBBBBBBBB...
    CCCCCC
    DDDD

    As you can see,

    String more than 10, having this - ...

    Less than 10, no need ...

    How the Query looks like ? Please help

  • select iif(len(first_name) > 10, concat(SUBSTRING(first_name, 1, 10),'...'),SUBSTRING(first_name, 1, 10)) as first_name from @t1

    Adi

  • SELECT LEFT(first_name, 10) + CASE WHEN LEN(first_name) > 10 THEN '...' ELSE '' END AS first_name 
    FROM @t1

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • select left(isnull(stuff(first_name, 11, 3, '...'), first_name), 13) 
    from @t1;

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Thanks to all of you

Viewing 5 posts - 1 through 4 (of 4 total)

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