SQL Serverを使用して文字列を切り捨てる方法 質問する

SQL Serverを使用して文字列を切り捨てる方法 質問する

SQL Server に大きな文字列があります。その文字列を 10 文字または 15 文字に切り捨てたい

元の文字列

this is test string. this is test string. this is test string. this is test string.

希望する文字列

this is test string. this is ......

ベストアンサー1

長い文字列のうちいくつかの文字だけを返したい場合は、次のようにします。

select 
  left(col, 15) + '...' col
from yourtable

見るデモ付きSQLフィドル

これは文字列の最初の 15 文字を返し、それを...末尾に連結します。

15 未満の文字列が取得されないようにするには、...次を使用します。

select 
  case 
    when len(col)>15
    then left(col, 15) + '...' 
    else col end col
from yourtable

見るデモ付きSQLフィドル

おすすめ記事