I have small question about SQL Server, please tell me how to solve this issue
Table: emp
id name
---------------
1 abc_rao
2 nani
3 hari_babu
4 kalibabu
5 ab_tan
Based on that table I want output like below
id firstname lastname
1 abc rao
2 nani nothing
3 hari babu
4 kalibabu nothing
5 ab tan
and I tried like this:
select
SUBSTRING(name, 1, CHARINDEX('_', name) - 1) as firstname ,
SUBSTRING(name, CHARINDEX('_', name) + 1, LEN(name)) as lastname
from emp
but I'm not getting exactly the expected result.
Instead, I'm getting an error:
Msg 537, Level 16, State 2, Line 3
Invalid length parameter passed to the LEFT or SUBSTRING function.
Please tell me how to solve this
Issue using query in SQL Server