0

I know you could Truncate a Table or Table variable but is there a way to also truncate a variable ?

DECLARE @HiddenMe NVARCHAR(MAX)

SET  @HiddenMe = (select XYZ.abc)

If LEN(@HiddenME) >= 1800000  Then would like to truncate @HiddenMe . 

Please let me know . FYI, am using sql 2008

4

2 回答 2

2

By truncate, I assume you mean truncating all data past a certain point, not dropping all data (like a table truncate). This should work.

SET  @HiddenMe=SUBSTRING(@HiddenMe,1,1800000)
于 2013-01-04T19:34:33.983 回答
1

You can use LEFT():

set @HiddenME = left(@HiddenME, 1800000)

So your script will be like this:

DECLARE @HiddenMe NVARCHAR(MAX)

SET  @HiddenMe = 'lets test a longish string to trim after a certain number of characters'

if LEN(@HiddenME) >= 1800000
  set @HiddenME = left(@HiddenME, 1800000)

See SQL Fiddle with Demo

于 2013-01-04T19:35:06.577 回答