In my database MYDB i have a table called MYTABLE and inside it I have a column called NAME. I want to only return the first 100 character开发者_Go百科s of the column NAME. (NAME can be up to 2000 characters).
How can this be done in SQL as I want to set the first 100 characters to a ASP.NET label.
Thanks in advance!
select left(NAME, 100) as Name, ... from MYTABLE...
You can use the LEFT
function, e.g.
SELECT LEFT(mt.NAME, 100) AS SHORTNAME FROM MYTABLE mt
Use Substring:
SELECT SUBSTRING(NAME, 1, 100) AS [ShortName]
FROM MYTABLE
SELECT SUBSTRING( NAME, 0 , 100 ) FROM MYTABLE
Use SUBSTRING function:
SELECT SUBSTRING(NAME, 1, 100) AS LABEL FROM MYTABLE
Use the LEFT
function:
SELECT LEFT(NAME, 100) AS NAME FROM MYTABLE
I like it with dots to show that there is more text
SELECT
CASE
WHEN LEN(NAME) <= 100 THEN NAME
ELSE LEFT(NAME, 97) + '...'
END SHORTNAME
FROM TABLE
精彩评论