Closed. This question needs details or clarity . It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post .
Closed 4 hours ago.
The community is reviewing whether to reopen this question as of 3 hours ago.
Improve this question
I need to extract the second part from text after the dash-
For example, Segment_Name
needs to become Segment
SELECT
s.segment_name,
LTRIM(SUBSTRING(s.segment_name, LEN(s.segment_name) - 7, LEN(s.segment_name))) AS Segment
FROM
[prod_idw_mart].[t].[dim_segment] s
This works as such except that the -7 is too short. Strangely enough that works in SAS perfectly. But in SQL, the second part of the words are cut short.
There is no specific length I can assign, so I was looking for another solution.
SELECT
s.segment_name,
RIGHT(s.segment_name, CHARINDEX('-', REVERSE(s.segment_name)) - 1) AS S
FROM
[prod_idw_mart].[timesheet].[dim_segment] s
Here I get an error:
Invalid length parameter passed to the RIGHT function.
Instead of -1, I tried > 0, but it does not like that at all:
Incorrect syntax near '>'.
2条答案
按热度按时间px9o7tmv1#
yyyllmsg2#
Try the following:
Demo
Note:
This supposes only one delimiter in the string. For the translate function, you may add extra delimiters to the ',:' list, and if your version of SQL Server doesn't support this function you may use multiple replace functions instead.