I've declared a variable in a stored procedure:
DECLARE @CurrentChunk NVARCHAR(250) I would like to use the length of the variable, i.e. 250, later in my sp for computational purposes, and I want to keep my code as dry as possible.
Here's my code (assume @Narrative is a param to the SP):
DECLARE @ChunkSizeCharacters INT, @NumChunks INT, @LoopIndex INT, @CurrentChunk NVARCHAR(250) SET @ChunkSizeCharacters = 250 -- HERE'S WHERE I WANT THE LENGTH OF @CurrentChunk SET @NumChunks = CEILING((LEN(@Narrative) * 1.0)/@ChunkSizeCharacters) SET @LoopIndex = 0; WHILE (@LoopIndex < @NumChunks) BEGIN SET @CurrentChunk = SUBSTRING(@Narrative, ((@LoopIndex * @ChunkSizeCharacters) + 1), @ChunkSizeCharacters) INSERT INTO [dbo].[Chunks] ([Chunk]) VALUES (@CurrentChunk) SET @LoopIndex = @LoopIndex + 1 END Is there a way to ascertain the length of an NVARCHAR or VARCHAR variable definition (please read carefully -- I'm not looking for LEN())?
@CurrentChunk, i.e. to show that I've actually contrived a computation that uses that value.