Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to Use Right () with Integers?

Author  Topic 

sqlnoob35
Starting Member

10 Posts

Posted - 2009-09-15 : 10:48:59
Hey guys,

I am running a query that returns a person's social security number. I want the query to display only the last 4 digits of the social. I know the RIGHT() function can do this with string values, but this is an integer.

Any way to do this?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-15 : 10:53:44
social security number is an integer ?

select int_col % 10000


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sqlnoob35
Starting Member

10 Posts

Posted - 2009-09-15 : 15:30:40
Yes, the datatype is int. Can you explain what "% 1000" is??
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-15 : 20:55:47
% is Modulo operator
please refer to BOL http://msdn.microsoft.com/en-us/library/ms190279%28SQL.90%29.aspx


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

nirene
Yak Posting Veteran

98 Posts

Posted - 2009-09-16 : 00:30:03
You can also use this

Select Right(Convert(Varchar,Field_Name),4)
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-16 : 00:34:58
quote:
Originally posted by nirene

You can also use this

Select Right(Convert(Varchar,Field_Name),4)



unless you want the result in string else you will need another convert() to convert back to integer


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-09-16 : 02:11:32
Khtan, right should be used. See below example.
DECLARE	@SSN INT

SET @SSN = 562530345

SELECT @SSN % 10000 AS Modulo,
RIGHT(@SSN, 4) AS [Right]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-16 : 02:16:11
isn't this suppose to be handle at presentation layer




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-09-16 : 02:18:38
Could be!
If you want to send the complete SSN to the application anyway.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -