Monday, March 19, 2012

DECODE please help

Hi-

I am trying to accomplish this in my SELECT statement...

If the length of the retreived string data is more than 10 characters, it should return the first 10 characters followed by a literal string '..' else return the string data as is

I tried to use IIF, CASE but didn't got it work, kept getting errors...

SELECT FinalName = IIF ( DATALENGTH ( NameString ) > 10, SUBSTRING ( NameString, 0, 10 ) + '..' , NameString ) FROM SomeTable

Any help is highly appreciated... Thanks for your quick responses...T-SQL does not provide an IIF function. You need to use CASE.


SELECT
CASE
WHEN LEN(NameString) > 10 THEN SUBSTRING ( NameString, 0, 10 ) + '..'
ELSE NameString
END AS FinalName
FROM
SomeTable

Terri|||And actually, for the SUBSTRING function you should be using a 1,10 not 0,10. And note that the LEN function would be more correct for your purposes than DATALENGTH.

Terri|||Thank you so very much Terri... This worked perfect...
:)

No comments:

Post a Comment