用SQL读取数据控制长度

xujun5031 2010-02-02 01:13:45
select title from news 比如读取title字段的值,长度超过20就用“...”表示,用SQL语句怎么写啊
...全文
135 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
昕颖 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jenny0810 的回复:]
SQL codeselect (casewhenlen(title)>20thensubstring(title,0,19)+'...'ELSE titleEND)as title
form news
[/Quote]正确
jack15850798154 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 seesea125 的回复:]
select
case title
when len(title)>20 then SUBSTRING(title,0,19)+'...'
ELSE title
END
form news

[/Quote]
up
jenny0810 2010-02-02
  • 打赏
  • 举报
回复
修改一下
[Quote=引用 8 楼 jenny0810 的回复:]
SQL codeselect (casewhenlen(title)>20thensubstring(title,0,19)+'...'ELSE titleEND)as title
FROM news
[/Quote]
jenny0810 2010-02-02
  • 打赏
  • 举报
回复

select (case when len(title)>20 then substring(title,0,19)+'...'
ELSE title END) as title
form news

izeroed 2010-02-02
  • 打赏
  • 举报
回复
大可不必这样 如果楼主要做web程序直接在css里设置样式就能达到这个效果
bobo_包子 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 nosuchtracter 的回复:]
最近熊猫特别多啊
因为熊猫容易出国的缘故?
[/Quote]
你也不赖啊
xujun5031 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyq11 的回复:]
select (case title when len(title)>20 then substring(title,0,19)+'...'
ELSE title END) as title
form news

[/Quote]显示错误??
消息 170,级别 15,状态 1,第 1 行
第 1 行: '>' 附近有语法错误。
xray2005 2010-02-02
  • 打赏
  • 举报
回复
1楼可以,

另外,还可以写个公共的方法,截取指定长度的字符串并加...,然后再需要的地方调用。

比如:

public static string CutString(string input,int MaxLength)
{
if(input.Length>MaxLength)
{
return input.SubString(0,MaxLength)+"...";
}
else
{
return input;
}
}
wuyq11 2010-02-02
  • 打赏
  • 举报
回复
select (case title when len(title)>20 then substring(title,0,19)+'...'
ELSE title END) as title
form news
nosuchtracter 2010-02-02
  • 打赏
  • 举报
回复
最近熊猫特别多啊
因为熊猫容易出国的缘故?
seesea125 2010-02-02
  • 打赏
  • 举报
回复
select
case title
when len(title)>20 then SUBSTRING(title,0,19)+'...'
ELSE title
END
form news

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧