user1681514
user1681514

Reputation: 97

SQL Server Variable Truncate

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

Upvotes: 0

Views: 448

Answers (2)

Taryn
Taryn

Reputation: 247810

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

Upvotes: 1

UnhandledExcepSean
UnhandledExcepSean

Reputation: 12804

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)

Upvotes: 2

Related Questions