Monday, March 26, 2012

RIGHT(string, #), I am not getting whats expected!

I have the following

SELECT @.cLastBarcode
SELECT @.tmpCount = RIGHT(@.cLastBarcode, 4)
SELECT @.tmpCount

SELECT @.cLastBarcode returns '14001DT0010006'

BUT

SELECT @.tmpCount returns nothing. The RIGHT(...) function does not render any results. I am expecting '0006'.

I read that the data type must be compatible with varchar. The @.cLastBarcode was declare as char(25). I have even tried casting the @.cLastBarcode char string to type varchar.

Any hints?

Mike BFind the difference:

SET ANSI_PADDING OFF
declare @.cLastBarcode char(25),@.tmpCount varchar(25)
set @.cLastBarcode='1234567890'
SELECT @.cLastBarcode
SELECT @.tmpCount = RIGHT(@.cLastBarcode, 4)
SELECT @.tmpCount
go
SET ANSI_PADDING ON
declare @.cLastBarcode char(25),@.tmpCount varchar(25)
set @.cLastBarcode='1234567890'
SELECT @.cLastBarcode
SELECT @.tmpCount = RIGHT(@.cLastBarcode, 4)
SELECT @.tmpCount|||Find the difference:

SET ANSI_PADDING OFF
declare @.cLastBarcode char(25),@.tmpCount varchar(25)
set @.cLastBarcode='1234567890'
SELECT @.cLastBarcode
SELECT @.tmpCount = RIGHT(@.cLastBarcode, 4)
SELECT @.tmpCount
go
SET ANSI_PADDING ON
declare @.cLastBarcode char(25),@.tmpCount varchar(25)
set @.cLastBarcode='1234567890'
SELECT @.cLastBarcode
SELECT @.tmpCount = RIGHT(@.cLastBarcode, 4)
SELECT @.tmpCount

Thanks, you are right on the money!. Good eye. I fixed it by changing
SELECT @.cLastBarcode = Barcode

TO

SELECT @.cLastBarcode = RTRIM(Barcode)

I am not sure where I read this, maybe BOL, but it was not advisable to turn off ansi_padding.

Thanks for your reply.

Mike B

No comments:

Post a Comment