1

Hi I have a column called mix which is as follows

120
102 201
300
234 212
11 21

So the issue is I want to extract the digits of the left when a space is found, I am trying that with substring as shown below, I wonder why it is not working.

 select mix,  
 SUBSTRING(mix,1,CHARINDEX(' ',mix)-1)
 FROM tbl_xx
 where CHARINDEX(mix,' ')>0
0

3 Answers 3

4

Find string comes first in CHARINDEX... Change it in WHERE condition

SELECT mix, SUBSTRING(mix, 1, CHARINDEX(' ',mix) - 1)
FROM   tbl_xx
WHERE  CHARINDEX(' ', mix) > 0
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

select mix,  
 SUBSTRING(mix,1,CHARINDEX(' ',mix)-1)
 FROM tbl_xx
 where CHARINDEX(' ',mix)>0

Your where clause charindex is wrong check the above

Comments

0

Use LEFT

select mix,LEFT(mix,CHARINDEX(' ',mix)-1)
 FROM tabl
 where CHARINDEX(' ',mix)>0

Comments

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.