If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Why Am I an Error with this Query?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-25-11, 10:28
dvdaddict32 dvdaddict32 is offline
Registered User
 
Join Date: Mar 2010
Posts: 32
Why Am I an Error with this Query?

Hey guys,

I am trying to run a query that will return me just the last four digits of a person's social security number. I tried the following in my SELECT statement:


SELECT Distinct right(A.MBR_SSN_NBR, 4 ) as SSN


Unfortunately, I get an error complaining that the data type or length of RIGHT is incorrect.

It looks like this function only works on character fields. If I use


SELECT Distinct right('A.MBR_SSN_NBR', 4 ) as SSN


The query runs and returns _NBR.



IS there something I'm missing here or is there a way to convert the SSN to character in my query?
Reply With Quote
  #2 (permalink)  
Old 08-25-11, 10:33
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
You should use
Code:
 
right(char(A.MBR_SSN_NBR),4)
As a hack, you can also use
Code:
 
MOD(A.MBR_SSN_NBR,10000)
I assume the latter one will be more efficient

HTH
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.

Last edited by sathyaram_s; 08-25-11 at 10:37. Reason: removed [quote] and added [code]
Reply With Quote
  #3 (permalink)  
Old 08-25-11, 10:38
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
It looks like this function only works on character fields.
You are right!

Quote:
... is there a way to convert the SSN to character in my query?
You may want to use DIGITS function like ...
Code:
SELECT DISTINCT
       RIGHT( DIGITS(a.mbr_ssn_nbr), 4 ) AS ssn
...
Note: function CHAR(number) may return a value with left justified.
So, RIGHT( CHAR(...), 4 ) may return a value including some trailing blanks.

Last edited by tonkuma; 08-25-11 at 10:45. Reason: Add note.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On