开发者

Silly TSQL Question - Taking (N) Characters in Select Statement

开发者 https://www.devze.com 2023-03-26 03:48 出处:网络
I have a select statement where I want to take 100 characters from a field. Is there an easy way to do this?

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...
0

精彩评论

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