VARCHAR

  • How can I use the VARCHAR command only to display the first 20 characters for a comment field that is set for varchar(200)?

    I am trying to display the first 20 characters and rest of the characters if the comment field if the string is longer then 20, to display as ....... dots.

  • joeSS wrote:

    How can I use the VARCHAR command only to display the first 20 characters for a comment field that is set for varchar(200)?

    I am trying to display the first 20 characters and rest of the characters if the comment field if the string is longer then 20, to display as ....... dots.

    Display them where?  If this is for a report it should be done in the report and not from SQL.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • declare @len int=20declare @table table (id int identity(1,1), strin varchar(202))insert into @table select 'antidisestablishmentarianism' union allselect 'thisisatestfieldof20' union allselect 'elephant' union allselect 'otorhinolaryngological'select len(strin),strin,concat(left(strin,@len),replicate('.',len(strin)-@len)) from @table
  • SELECT LEFT(x.Comment,20) + IIF(LEN(x.Comment>20,'...','')
    FROM myTable x

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

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