I either need a resolution to what I have or a cleaner way to get the desired results and I've looked around, I keep getting a "EUREKA" moment only to be disappointed that the results are still the same.
I'm trying to get a 3 digit number from a longer string. It's a varchar column. The last three digits are key to the uniqueness of the record.
select max(right(CardNumber,3)) as cardprefix
from tblVSKB_CardData
where partnumber = 'N0090026'
group by PartNumber
This returns a '002', I need to increment that number but when I do...
select max(right(CardNumber,3))+1 as cardprefix
from tblVSKB_CardData
where partnumber = 'N0090026'
group by PartNumber
I get '3'. I need it to be '003'.
So I tried taking the results and casing it with the LEN function. (This probably was a terribly bad idea in trying to keep it clean and that's where I would appreciate some help in that regard.)
select
case when len(max(right(CardNumber,3))+1) = 1
then '00'+(
select max(right(CardNumber,3))
)
end
as prefix
from tblVSKB_CardData
where partnumber = 'N0090026'
group by PartNumber
The beauty here is that the result was '00002' That was a "EUREAK!!" moment. However, when I add that fateful +1 to the mess, I'm still getting one digit.
select
case when len(max(right(CardNumber,3))+1) = 1
then '00'+(
select max(right(CardNumber,3))+1
)
end
as prefix
from tblVSKB_CardData
where partnumber = 'N0090026'
group by PartNumber
The result is still '3'. I know it's dropping the concatenation because when the prefix is a returned value like 122 and the case is adjusted to ...len(max(right(CardNumber,3))+1) = 3..., I still just get 123. But when the THEN clause has the +1 removed, it returns a '00122'. Keep in mind this won't be scenario I just used it as a test.
Please help. I'm way behind on getting this thing off the ground.
Thanks,
Rog...
I'm trying to get a 3 digit number from a longer string. It's a varchar column. The last three digits are key to the uniqueness of the record.
select max(right(CardNumber,3)) as cardprefix
from tblVSKB_CardData
where partnumber = 'N0090026'
group by PartNumber
This returns a '002', I need to increment that number but when I do...
select max(right(CardNumber,3))+1 as cardprefix
from tblVSKB_CardData
where partnumber = 'N0090026'
group by PartNumber
I get '3'. I need it to be '003'.
So I tried taking the results and casing it with the LEN function. (This probably was a terribly bad idea in trying to keep it clean and that's where I would appreciate some help in that regard.)
select
case when len(max(right(CardNumber,3))+1) = 1
then '00'+(
select max(right(CardNumber,3))
)
end
as prefix
from tblVSKB_CardData
where partnumber = 'N0090026'
group by PartNumber
The beauty here is that the result was '00002' That was a "EUREAK!!" moment. However, when I add that fateful +1 to the mess, I'm still getting one digit.
select
case when len(max(right(CardNumber,3))+1) = 1
then '00'+(
select max(right(CardNumber,3))+1
)
end
as prefix
from tblVSKB_CardData
where partnumber = 'N0090026'
group by PartNumber
The result is still '3'. I know it's dropping the concatenation because when the prefix is a returned value like 122 and the case is adjusted to ...len(max(right(CardNumber,3))+1) = 3..., I still just get 123. But when the THEN clause has the +1 removed, it returns a '00122'. Keep in mind this won't be scenario I just used it as a test.
Please help. I'm way behind on getting this thing off the ground.
Thanks,
Rog...