views:

38

answers:

4

I use this mysql statement for concating description which has a length greater than 30..

select if (CHAR_LENGTH(description)>30,CONCAT(SUBSTRING(description,1,30),
'.....'),description) as description from table

How to change this mysql select into sql server select statement?

A: 

SELECT LEFT(description,30) as description FROM table

RandyMorris
@Randy how to concat `....` string...
Pandiya Chendur
SELECT LEFT(description,30)+'.....' as description FROM table
RandyMorris
A: 

Use a CASE statement; something like:

SELECT 
    CASE WHEN CHAR_LENGTH(description) > 30 
    THEN SUBSTRING(description,1,30) + '.....'
    ELSE description
    END as description 
FROM 
    table
Adam Bernier
+1  A: 
select 

    description = 

       case when LEN(description) > 30 then
           SUBSTRING(description,1,30) + '.....'
       else
           description 
       end

from table
Michael Buen
A: 
SELECT
    CAST(Description AS VARCHAR(30))
    +
    CASE WHEN DATALENGTH(Description) > 30 THEN '...' ELSE '' END AS Description
FROM table

This is NULL-proof and works for text datatypes.

Salman A