I have a select statement where I want to take 100 characters from a field. Is there an easy way to do this?
Here is some pseudo code below.
Select substring(0, 100, longFie开发者_如何学编程ld)
from myTable
You've got it. Use the SUBSTRING()
method. This will work on any string/character/binary/image datatypes.
DECLARE @str varchar(1000);
SELECT @str = 'foobar-booz-baz-cowboys';
SELECT SUBSTRING (@str, 0, 10);
-- returns foobar-boo
--from a table:
SELECT SUBSTRING(CustomerName,0,100)
FROM MyTable;
Your pseudocode is pretty close.
select substring(longField, 0, 100)
from myTable
(Just for reference)
SQL Substring::
substring(expression, starting_pos, length)
The easiest way is using LEFT (Transact-SQL):
SELECT
LEFT(longField,100) AS longField, ...
FROM myTable
WHERE...
精彩评论