0

I have string column email_id; the data will look like this:

email_id 
"1"
"6"
"3 4"
"8"
"0 3"
"0 5 7"

I want to get list of ids as integer. If I have two numbers in my string, I want the last one. My result should look like;

SELECT some_function (email_id ) FROM table
1
6
4
8
3
7

Is it possible to do this in SQL Server?

6
  • You haven't explained why "3 4" is parsed as 4 and "0 3" as 3. Are these space-delimited lists where you always take the last integer? If so, CHARINDEX and SUBSTRING can help you. If not, you need to explain your logic. Commented Dec 9, 2011 at 10:15
  • I just re-read it, the OP does say "the last one" Commented Dec 9, 2011 at 10:16
  • If i have two numbers in my string, I want the last one. Commented Dec 9, 2011 at 10:17
  • 1
    Will it ALWAYS be either one or two numbers? Commented Dec 9, 2011 at 10:18
  • 4
    This is not the correct way to represent a one to many relationship. You should have a separate table with one row for each entityId, emailId Commented Dec 9, 2011 at 10:23

2 Answers 2

5
SELECT
  CAST(RIGHT(email_id, LEN(email_id) - CHARINDEX(' ', email_id)) AS INT)
FROM
  yourTable

IF and ONLY IF, all your values can reliably be cast to an INT, and there is only ever one space at most.


EDIT To deal with a list of n values

This isn't pretty, but it avoid recurrsion and/or loops. If someone gives an answer without REVERSE() test to see if it's faster than this or not.

SELECT
  CAST(
    REVERSE(
      LEFT(
        REVERSE(email_id),
        CHARINDEX(' ', REVERSE(email_id) + ' ') - 1
      )
    )
    AS INT
  )
FROM
  yourTable
Sign up to request clarification or add additional context in comments.

6 Comments

This is working if i have only two values. Anyway it was helpful, Now I'mtrying to edit your SQL to make it work for more than two numbers...thx
@Ba1a - Updated using REVERSE() and LEFT() to take the last item, regardless of how many items there are.
select CAST(REVERSE (LEFT ( REVERSE ('3 4 15'), CHARINDEX(' ', REVERSE ('3 4 15')))) AS INT) This itself working fine...I didn't understand why did you add space in the reversed email-id.
The + ' ' is needed for when there is no space in the string (You'll get CAST('' AS INT) otherwise). And the - 1 is there for 'purity sake' to avoid having a space in the string being CAST to an INT.
Ok. I was handling single number by CASE statement like ; SELECT CASE WHEN LEN('3 4 15')> 1 THEN CAST(REVERSE (LEFT ( REVERSE ('3 4 15'), CHARINDEX(' ', REVERSE ('3 4 15')))) AS INT) ELSE CAST('3 4 15' AS INT) END
|
-2
SELECT CAST(replace(your_column ,' ','') as int)  FROM table

5 Comments

select CAST('3 4' AS INT) -- This row will fail ..I need to get 4 as value for this row
@Dezigo - And for the records that have multiple IDs separated by a space?
This will fail on values like "3 4"
u need to add replace function
Dems - oh. OK. I have missed it.(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.