0

i am writting sql script for the first time and i want to seperate a value that contains two user id's which looks like this

612831408762037_617540318291146

i want to separate these two so i have a user_id = 612831408762037 and post_id = 617540318291146 any ideas on how i shuld approch this?

thanks in advance

rinuthomaz
  • 1,393
  • 2
  • 23
  • 38
user3659384
  • 37
  • 2
  • 8
  • check this : http://stackoverflow.com/questions/2696884/mysql-split-value-from-one-field-to-two – Amar Oct 22 '14 at 05:27
  • @user3659384, Please accept the answer which worked for you, it will be easy for other people who refers this question. – rinuthomaz Oct 23 '14 at 05:03

3 Answers3

1

Please try:

declare @var nvarchar(100)='612831408762037_617540318291146';

select 
    LEFT(@var, charindex('_',@var)-1), 
    RIGHT(@var, charindex('_',REVERSE(@var))-1)
TechDo
  • 18,398
  • 3
  • 51
  • 64
0

You could use a substr('value', start, length) for each portion that you want displayed. The substr function extracts and returns a specific segment from a string or value.

  • You probably could achieve this by using a weird concat statement as well.
execv3
  • 312
  • 1
  • 4
  • 14
0

Please try this.

DECLARE @document varchar(400);
DECLARE @FIRST int;

SELECT @document = '612831408762037_617540318291146234234234234';
SET @FIRST = (SELECT CHARINDEX('_', @document, 1));
SELECT rightSide = SUBSTRING(@document, 0,@FIRST), leftSide = SUBSTRING(@document, @FIRST+1, LEN(@document));
rinuthomaz
  • 1,393
  • 2
  • 23
  • 38