Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

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
0
brgdotnet
Asked:
brgdotnet
5 Solutions
 
HuaMinChenBusiness AnalystCommented:
Try
cast('1234' as int)

Open in new window

0
 
pven13Commented:
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
brgdotnetAuthor 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
 
pven13Commented:
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now