'Getting the second occurrence from charindex function in sql server
I need to get the second occurrence of the space for below text. It should be the space after the 56, but I'm getting 15th position before the 56 as the first one.
select charindex(' ', 'Posted/edited: 56 days ago', 2)
Solution 1:[1]
You are already getting position of the second space (' ') in your query => 15. To clarify for example, you can use it to extract content from that point onwards, using following
select substring('Posted/edited: 56 days ago',
charindex(' ', 'Posted/edited: 56 days ago', 2) + 1,
len('Posted/edited: 56 days ago'))
Solution 2:[2]
Setting a start location is an excellent idea. However I would use the searchable character first occurrence +1 as a start location as situation may vary. So the solution would be:
Select charindex(' ','Posted/edited: 56 days ago',(charindex(' ', 'Posted/edited: 56 days ago') +1))
The output is ofc 18
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gro |
| Solution 2 | Anisa Mitre |
