Converting a varchar(4) to an integer in sql

I have a table with several columns. One of the columns is named FITS_SUFFIX, and is of type VARCHAR(4). If I look at the data in that column using
a sql statement, I get a 4 digit string of alphabet characters, such as ABCW, BERU, KAPI, JMUU

Now what confuses me, is that in this sql stored procedure I am maintaining, they are converting this 4 digit character to an integer.
See  lines 3, 9, and 27 below. Then at line 34 they use this converted character value in an algorithm.

So what happens when you convert a character string like ABCW to an integer? Does it just convert the first character? I tried to do something like
that from the query analyzer command line, but kept getting errors. Anyway, I truly need expert help on this.

1 DECLARE CURS_STATE CURSOR
 2 FOR
 3    SELECT FITS_STATE_CD, FITS_PREFIX, FITS_SUFFIX
 4    FROM SMT_FCTS_FITS_STATE_CDS FITS
 5   WHERE FITS.FITS_ACTIVE = 1
 6   ORDER BY FITS_STATE_CD

 7 OPEN CURS_STATE
 8 FETCH NEXT FROM CURS_STATE
 9 INTO @lchStateCode, @lchPrefix, @lchSuffix

 10  WHILE @@FETCH_STATUS = 0
 11  BEGIN
 12     DECLARE CURS_EMP CURSOR
 13     FOR
 14     SELECT
 15     USTY.PSUS_USER_NUM AS PSUS_USER_NUM,
 16     USTY.FITS_STATE_CD AS FITS_STATE_CD,
 17     FITS.FITS_PREFIX AS FITS_PREFIX
 18     FROM SMT_FCTS_FUAD_FOOTETS_USR USTY,
 19     SMT_FCTS_FITS_STATE_CDS FITS
 20     WHERE USTY.FITS_STATE_CD = FITS.FITS_STATE_CD
 21     AND FITS.FITS_ACTIVE = 1
 22     AND USTY.FITS_STATE_CD = @lchStateCode
 23     AND (USTY.FUAD_ITS_CLERK_ID = '' OR USTY.FUAD_ITS_CLERK_ID IS NULL)
 24     AND USTY.PSTE_CODE = @lchProdSteCode
 25     ORDER BY USTY.PSUS_USER_NUM



 26     OPEN CURS_EMP
 27     SELECT @lchFITS_SUFFIX = CONVERT(INT, @lchSuffix)
 28     FETCH NEXT FROM CURS_EMP
 29     INTO @lchPSUS_USER_NUM, @lchFITS_STATE_CD, @lchFITS_PREFIX

 30     WHILE @@FETCH_STATUS = 0
 31     BEGIN
 32        SELECT @lchFITS_SUFFIX = @lchFITS_SUFFIX+1

 33      UPDATE SMT_FCTS_FUAD_FOOTETS_USR SET
 34        FUAD_ITS_CLERK_ID = @lchFITS_PREFIX + (SELECT CHAR(65 +(@lchFITS_SUFFIX%17576) / 676)
 35       WHERE PSUS_USER_NUM = @lchPSUS_USER_NUM
 36       AND (FUAD_ITS_CLERK_ID = ' ' OR FUAD_ITS_CLERK_ID IS NULL


 37        FETCH NEXT FROM CURS_EMP
 38        INTO @lchPSUS_USER_NUM, @lchFITS_STATE_CD, @lchFITS_PREFIX

 39       END

 40   CLOSE CURS_EMP
 41   DEALLOCATE CURS_EMP
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HuaMin ChenProblem resolverCommented:
Try
cast('1234' as int)

Open in new window

0
Peter van der VenCommented:
Is there some kind of error handling in this stored procedure? Maybe at runtime the conversion results in an error as well, but you don't notice it because of the error handling.

What is the datatype of @lchSuffix?

Maybe you can post all lines of this stored procedure.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Didn't you ask a similar question before?
This can't run without an error. Maybe the person that wrote the code wanted to select another column and not that one. Or he was expecting that the column having only numbers.
0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

brgdotnetcontractorAuthor Commented:
Well apparently the stored procedure has been running fine, so I don't see how it is causing an error. Are you saying that the conversion to an int will cause the error?

Can we just focus on the conversion for now. What happens when the varchar 4 is converted to an int? Is that what you say will cause the error?

The stored procedure is larger than what I displayed here. I can't cut and paste it, and have to manually type it in, which would take over an hour.
0
Peter van der VenCommented:
What happens when the varchar 4 is converted to an int? Is that what you say will cause the error?

I can't think of a way to convert 'ABCW' to an int value without causing an error... Can you please check if the error is intercepted by error handling? Does your stored procedure produce any logging?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
You can make a simple test:
DECLARE @MyVarchar VARCHAR(4)

SET @MyVarchar = 'ABCW'

SELECT CONVERT(INT, @MyVarchar)

Open in new window

This will return the error: "Conversion failed when converting the varchar value 'ABCW' to data type int."
0
Scott PletcherSenior DBACommented:
What is the datatype of:
@lchSuffix

You're not adding 1 directly to FITS_SUFFIX, you're adding 1 to the variable which FITS_SUFFIX gets FETCH'ed into.

I strongly suspect it's binary/varbinary, based on the code overall, esp. this line:
FUAD_ITS_CLERK_ID = @lchFITS_PREFIX + (SELECT CHAR(65 +(@lchFITS_SUFFIX%17576) / 676)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.