I have a column which should contain text relatively similar to this
Storage Location: E10-2
Die Area: 28.90
Really all I care about is the "E10-2" portion.
The code I am posting below is the best I have come up with and I am cringing just looking at it.
First off, I'm relying on the fact that the lines have not been reversed. Second, I'm relying on two spaces preceding and a carriage return following the location. Finally this a horribly long and cumbersome select string.
I looked into using dynamic SQL but that seemed even more cumbersome given the size of the size of the "FROM" statement.
Am I wrong? Is there a more elegant way to accomplish this? Possibly validating part of the "Storage Location:" line so I know I'm looking at the right line of the field?
SELECT SUBSTRING(
CONVERT(VARCHAR(MAX),CONVERT(BINARY(8000),RB.BITS)),
CHARINDEX(' ',CONVERT(VARCHAR(MAX),CONVERT(BINARY(8000),RB.BITS)),CHARINDEX(' ',CONVERT(VARCHAR(MAX),CONVERT(BINARY(8000),RB.BITS)))+1),
CHARINDEX(CHAR(10),CONVERT(VARCHAR(MAX),CONVERT(BINARY(8000),RB.BITS)))-CHARINDEX(' ',CONVERT(VARCHAR(MAX),CONVERT(BINARY(8000),RB.BITS)) ,CHARINDEX(' ',CONVERT(VARCHAR(MAX),CONVERT(BINARY(8000),RB.BITS)))+1))
FROM
TLS.DBO.REQUIREMENT R
INNER JOIN
TLS.DBO.REQUIREMENT_BINARY RB ON R.WORKORDER_BASE_ID = RB.WORKORDER_BASE_ID
AND R.WORKORDER_LOT_ID = RB.WORKORDER_LOT_ID
AND R.WORKORDER_SPLIT_ID = RB.WORKORDER_SPLIT_ID
AND R.WORKORDER_SUB_ID = RB.WORKORDER_SUB_ID
AND R.PIECE_NO = RB.PIECE_NO
WHERE
R.WORKORDER_BASE_ID = 'FP-01405-0032' AND
R.PART_ID LIKE 'PCH%'
maxunless you using SQL-CE.