In SQL Server how do I return part of a string? the data is not fixed however the number of characters is and its position in the string. for example I want to just return the N Prefix code that is 10 characters...
000010000100001LN000002496 00000 0496139796000000001101GOODS IN RM CHECK 003
SUBSTRING is your friend:
DECLARE @string VARCHAR(100)='000010000100001LN000002496 00000 0496139796000000001101GOODS IN RM CHECK 003';
SELECT SUBSTRING(@string,1,10);
The result
0000100001
Find details here: https://msdn.microsoft.com/en-us/library/ms187748.aspx
Please find the below generalized code which will get the data based on the position of the N.
declare @find varchar(100)='000010000100001LN000002496 00000 0496139796000000001101GOODS'
print LEFT(SUBSTRING (@find,1,charindex('N',@find,1)-1),10)
if you remove the LEFT function in the above sql, then it will give the complete prefix of the N in the string data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.