coder
coder

Reputation: 13248

Conversion failed when converting the nvarchar value "XXX" to data type int

I would like to convert the below query from nvarchar to int

SELECT 'A' + RIGHT('000' + CAST((MAX(UserID) + 1) as nvarchar(50)), 3) FROM users

How do I do that?

This is my table:

enter image description here

And this is my data:

ID  | UserID  | Name

1      A001    XYZ

Upvotes: 0

Views: 9723

Answers (1)

Ritesh Khatri
Ritesh Khatri

Reputation: 484

now try this one.

    Select 'A' + RIGHT('000' + 
    CAST((MAX(Convert(Int,SUBSTRING(USERID,2,LEN(USERID))))+ 1) as nvarchar(50)), 3)
    From users

Upvotes: 1

Related Questions