Getting the part for string using sql server query I have rows like below 

10470_AMK-4799__OFS_BROWSERTC
6038_DMUSER_DM.READ.REPLACEMENT
6043_DMUSER2__OFS_DM.OFS.SRC.VAL
10194_AMK-TC037__OFS_BROWSERTC
64948_AMK-TABLETUSER__OFS_TWS
10010_AMK-0357


I want to write a query which can produce the part of string like below (from the first underscore up to the second, both not included):

AMK-4799
DMUSER 
DMUSER2
AMK-TC037
AMK-TABLETUSER
AMK-0357


I am using Microsoft SQL Server 2014.
Assuming you want data between first instance of _ and second instance of _ OR length of string in-case second _ is not present.

Try this Query:

-- Create Table Variable
DECLARE @tblString TABLE(StringVal VARCHAR(512));
-- Insert Values in Table Variable
INSERT INTO @tblString(StringVal)
select '10470_AMK-4799__OFS_BROWSERTC' UNION ALL
select '6038_DMUSER_DM.READ.REPLACEMENT' UNION ALL
select '6043_DMUSER2__OFS_DM.OFS.SRC.VAL' UNION ALL
select '10194_AMK-TC037__OFS_BROWSERTC' UNION ALL
select '64948_AMK-TABLETUSER__OFS_TWS' UNION ALL
select '10010_AMK-0357';
-- Select and process for expected Result
SELECT SUBSTRING(
                  SUBSTRING(StringVal,CHARINDEX('_',StringVal,0)+1,LEN(StringVal))
                ,0
                ,CASE when CHARINDEX('_',SUBSTRING(StringVal,CHARINDEX('_',StringVal,0)+1,LEN(StringVal))) &lt;&gt; 0
                    then CHARINDEX('_',SUBSTRING(StringVal,CHARINDEX('_',StringVal,0)+1,LEN(StringVal)))
                    else LEN(StringVal) as Result
                end
                )
FROM @tblString


The results are as below:

Result
-------
AMK-4799
DMUSER
DMUSER2
AMK-TC037
AMK-TABLETUSER
AMK-0357


This will achieve current scope.