Solved

Converting a varchar(4) to an integer in sql

Posted on 2014-11-30
7
311 Views
Last Modified: 2014-12-04
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
Comment
Question by:brgdotnet
7 Comments
 
LVL 10

Assisted Solution

by:HuaMinChen
HuaMinChen earned 100 total points
ID: 40473135
Try
cast('1234' as int)

Open in new window

0
 
LVL 1

Assisted Solution

by:pven13
pven13 earned 100 total points
ID: 40473202
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
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 200 total points
ID: 40473241
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:brgdotnet
ID: 40473545
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
 
LVL 1

Expert Comment

by:pven13
ID: 40473563
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
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 200 total points
ID: 40473572
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
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 100 total points
ID: 40474075
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now