Click here to Skip to main content
15,918,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
as begin

  declare @lastval varchar(10)

  set @lastval = (select max(RealEstateNumber) from RlRegistrationRealEstate)

   if @lastval is null set @lastval = 'C0001'

   declare @i int set @i = right(@lastval,4) + 1 return 'C' + right('000' + convert(varchar(10),@i),4)

    end


UPDATE [dbo].RlRegistrationRealEstate SET RealEstateNumber=@i WHERE [GUID] = @strGUID
Posted
Updated 19-Apr-14 22:03pm
v2
Comments
Tomas Takac 19-Apr-14 17:19pm    
Can you be more specific what exactly is NULL and shouldn't be? In case you mean the RealEstateNumber is never updated then I'd like to point your attention to the RETURN statement just before END... BTW are you sure you cannot use identity for this?
Not clear.

1 solution

Have a look at example:
SQL
DECLARE @RegistrationTable TABLE (Code VARCHAR(30))

INSERT INTO @RegistrationTable(Code)
VALUES('C0001'), ('C0099'), ('C0589')

SELECT SUBSTRING('C0000', 1, 5-LEN(IntCode)) + CONVERT(VARCHAR(4),IntCode) AS NewCode
FROM (
SELECT COALESCE(MAX(CONVERT(INT, RIGHT(Code,4))),0)+1 AS IntCode
FROM @RegistrationTable
) AS T

Returned value: C0590.

I hope it helps ;)
 
Share this answer
 
Comments
[no name] 24-May-14 16:32pm    
I think you need an SQL- gun license. I downvotaed with 5 ;)
Maciej Los 24-May-14 18:03pm    
:laugh:
I really appreciate this 'downovote' ;)
SQL-gun license - i like it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900