Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Converting a varchar(4) to an integer in sql

Posted on 2014-11-30
7
Medium Priority
?
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 11

Assisted Solution

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

Open in new window

0
 
LVL 1

Assisted Solution

by:pven13
pven13 earned 400 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 52

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 800 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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 52

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 800 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 70

Accepted Solution

by:
Scott Pletcher earned 400 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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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.
Viewers will learn how the fundamental information of how to create a table.

715 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