开发者

Return first N characters of string

开发者 https://www.devze.com 2022-12-14 01:12 出处:网络
I need to select a shortened version of a field from a SQL Server table to use in a drop down list. This field has to be limited to twenty characters.If the field has more than twenty characters, it

I need to select a shortened version of a field from a SQL Server table to use in a drop down list.

This field has to be limited to twenty characters. If the field has more than twenty characters, it should display the first twenty; if it has开发者_如何学C less than twenty characters, it should display the whole string.

How do I do this?


Try left(sting_field,20) or right(sting_field,20)


This SELECT should do:

SELECT 
  SUBSTRING(ISNULL(stringfield, ''), 1, 20)

It will replace a "NULL" value with an empty string '' and limit length to 20 chars max.


You can use the LEFT command.


I am confused... why dont you have the code that populates the dropdown manage the length of the data being loaded.

If you must do it within a query you could simply do a substring on the column:

https://learn.microsoft.com/en-us/sql/t-sql/functions/substring-transact-sql

0

精彩评论

暂无评论...
验证码 换一张
取 消