Quantcast
Channel: Forum SQL Server Database Engine
Viewing all articles
Browse latest Browse all 15694

strange behavior with in-line string concatenation

$
0
0

Hello!

We recently upgraded to SQL 2012 (64 bit).

We are noticing a difference in behavior as it relates to string concatenation.

Part 1 of the following script creates a test table populates with the data.

Part 2 of the script populates a varchar variable with some values concatenated from the table, and works as expected.

Part 3 of the script populates a varchar variable with some values concatenated from the table, and does not work as expected.  The only different between part 2 nd part 3 is the order by clause.

Part 4 drops the index on the table.

Part 5 repeats Part 3, but it returns the proper results now that the index is gone.

Do you think this is a bug in the Database engine?

Thanks in advance.

Part 1:


create table dbo.test_table (id int, PaidToDate_ID int,
                             PaidToDate_Code varchar(3),
                             PaidToDate_Amount money)

create index ix_test_table
   on dbo.test_table (id)

insert into test_table
  select 1, 1, 100, 1000.00

insert into test_table
  select 1, 2, 100, 1500.00

insert into test_table
  select 1, 3, 100, 300.00

insert into test_table
  select 2, 1, 200, 2000.00

insert into test_table
  select 2, 2, 200, 3400.00

insert into test_table
  select 2, 3, 200, 200.00

insert into test_table
  select 2, 1, 300, 1100.00

insert into test_table
  select 2, 2, 300, 500.00

insert into test_table
  select 2, 1, 300, 333.00

Part 2:


declare @id integer
declare @paidtodatestring varchar(max)
set @id = 2
select @paidtodatestring = ''
select @paidtodatestring = @paidtodatestring +
e.paidtodate_code + replicate(' ',(3-len(e.paidtodate_code)))+  
replicate('0',(11-len(REPLACE(CAST(e.paidtodate_amount AS VARCHAR(11)),'.','')))) + REPLACE(CAST(e.paidtodate_amount AS VARCHAR(11)),'.','')
from dbo.test_table e
where e.id = @id
select @paidtodatestring

Part 3:

declare @id integer
declare @paidtodatestring varchar(max)
set @id = 2
select @paidtodatestring = ''

select @paidtodatestring = ''
select @paidtodatestring = @paidtodatestring +
e.paidtodate_code + replicate(' ',(3-len(e.paidtodate_code)))+  
replicate('0',(11-len(REPLACE(CAST(e.paidtodate_amount AS VARCHAR(11)),'.','')))) + REPLACE(CAST(e.paidtodate_amount AS VARCHAR(11)),'.','')
from dbo.test_table e
where e.id = @id
order by e.paidtodate_id
select @paidtodatestring

Part 4:

drop index ix_test_table on dbo.test_table

Part 5:

declare @id integer
declare @paidtodatestring varchar(max)
set @id = 2
select @paidtodatestring = ''
select @paidtodatestring = @paidtodatestring +
e.paidtodate_code + replicate(' ',(3-len(e.paidtodate_code)))+  
replicate('0',(11-len(REPLACE(CAST(e.paidtodate_amount AS VARCHAR(11)),'.','')))) + REPLACE(CAST(e.paidtodate_amount AS VARCHAR(11)),'.','')
from dbo.test_table e
where e.id = @id
order by e.paidtodate_id
select @paidtodatestring


Victor Ambruso


Viewing all articles
Browse latest Browse all 15694

Trending Articles