I have a query that returns a field that is stored like this year-month-info. So an example of data would be 2018-February-Full Page. I need to split this by the '-' value so instead of just the one column returned, I would get three columns.
Year Month Text
2018 February Full Page
The text is not fixed formatting to the catch would have to be by the '-' symbol and the data will always be split this way. How to do this easily?
EDIT:
Here is my code
Declare @Str varchar(80)
Select @Str = IPDesc from vw_MRA_AdContracts
Declare @first_dash int = CharIndex('-', @Str, 1) Declare @last_dash int = CharIndex('-', Reverse(LTrim(Rtrim(@Str))))
Select profileid, OrgName, @Str,
Substring(@Str, 1, @first_dash-1) as AdYear,
Substring(@Str, @first_dash+1, Len(@Str)-@first_dash-@last_dash) as AdMonth,
Substring(@Str, @last_dash+@first_dash, Len(@Str)) as AdSold,
from vw_MRA_AdContracts
The problem is that it looks like the variable isn't doing a loop through all records available and grabbing one and then splitting the one record. So for AdYear, AdMonth and AdSold, I am getting the exact same value for each returning record even if it doesn't match what that record has.