如果你不想超出字符數限制,你可以有這樣的:
DECLARE @Table TABLE
(
String nvarchar(100)
)
DECLARE
@Size int = 25
INSERT @Table SELECT 'Lorem ipsum dolor sit ame'
INSERT @Table SELECT 'Lorem ipsum dolor sit ame tas'
INSERT @Table SELECT 'Lorem ipsum dolor sit am asd'
INSERT @Table SELECT 'Lorem ipsum dolor sita am'
INSERT @Table SELECT 'Lorem ipsum dolor sita a amet, consectetur adipiscing elit,'
INSERT @Table SELECT 'Lorem ipsum dolor sita'
INSERT @Table SELECT 'Lorem ipsum dolor sita asamet, consectetur adipiscing elit,'
SELECT
LEN(R.LimitTruncation) AS LimitTruncationLen,
R.LimitTruncation,
LEN(String) AS StringLen,
R.String
FROM
(
SELECT
String,
--This is the main part
CASE
WHEN LEN(String) <= @Size THEN String
WHEN CHARINDEX(' ', String, @Size) = @Size OR CHARINDEX(' ', String, @Size) = @Size + 1 THEN RTRIM(LEFT(String, @Size))
ELSE REVERSE(SUBSTRING(REVERSE(LEFT(String, @Size)), CHARINDEX(' ', REVERSE(LEFT(String, @Size))), @Size))
END AS LimitTruncation
FROM
@Table
) R
這個工作預期。非常感謝! – user2146538 2013-04-09 22:22:04
現在,如果您也將他的答案標記爲正確,那麼他會因發佈答案而獲得一點積分。 – BerggreenDK 2013-08-21 12:24:28