Hi guys
I hope everything's ok with all of you
I have this example to do it in SQL. I want all characters after the space between. like this
Please beware the number of characters. The ideal would be something like "give all characters from space to the right"
I hope I was clear.
Thanks a lot in advance
October 31, 2022 at 6:03 pm
without giving you the code look at functions charindex, substring and reverse(use twice) - using these 3 you will be able to get that split
October 31, 2022 at 6:20 pm
Ok Frederico. I got this
SUBSTRING (Restaurant, CHARINDEX ('- ', Restaurant), 20) AS Restaurant
The result is this
- RestaurantName
I don't think I understood that REVERSE function because from what I've seen the result would be
emaNtnaruatseR -
I'm missing something, right?
October 31, 2022 at 6:57 pm
you missing the fact I said reverse (use twice)
October 31, 2022 at 7:42 pm
without giving you the code look at functions charindex, substring and reverse(use twice) - using these 3 you will be able to get that split
I believe that reverse is relatively expensive. I think it's simpler--and therefore easier--to use just charindex and stuff.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
October 31, 2022 at 8:31 pm
multiple ways - I was just giving one of the ways with an incentive to search for it and try to understand it.
and... substring vs stuff - which one is better performance? not sure if there are any testcases on that.
one thing that may mess up with Any of the ways is if there are cases where the string has more than 1 space (e.g. 3 words or more).
What should happen on that case Pedro
October 31, 2022 at 8:58 pm
Hi guys I hope everything's ok with all of you
I have this example to do it in SQL. I want all characters after the space between. like this
Please beware the number of characters. The ideal would be something like "give all characters from space to the right" I hope I was clear. Thanks a lot in advance
Two things...
--Jeff Moden
Change is inevitable... Change for the better is not.
Ah, I see, test driven development:
;with cte as
(
select * from (values ('Restaurant'),
('Restaurant Abc1'),
('Rest Abc'),
('Re Abc32'),
('Restau Abc4'),
('Restaurant Abbe'))T(C)
)
select C , right(C, len(C) - charindex(' ', C))
from cte
November 2, 2022 at 11:00 am
Hi Frederico,
I haven't missed that fact of using twice the REVERSE function but look at what I've said:
I wrote this: SUBSTRING (Restaurant, CHARINDEX ('- ', Restaurant), 20) AS Restaurant
and what I got was this
'- RestaurantName'
Even If I've used the REVERSE twice it wouldn't change what I'm looking at here...unless it should be used differently. And that's the help I ask you because I don't have enough knowledge to use that function properly.
Also, to answer your question, it's always just 2 words separated by a space dash space, like my example
Jonathan, I'm going to try your suggestion. I don't think it will work because it should be splited after ' - ' always and show me everything after that space dash space
Thanks a lot all of you for helping me
Still looking forward for the right solution
November 2, 2022 at 11:11 am
Using Jonathan's test data, here is the STUFF() version.
WITH cte
AS (SELECT *
FROM
(
VALUES
('Restaurant')
,('Restaurant Abc1')
,('Rest Abc')
,('Re Abc32')
,('Restau Abc4')
,('Restaurant Abbe')
) T(C) )
SELECT cte.C
,STUFF (cte.C, 1, CHARINDEX (' ', cte.C), '')
FROM cte;
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
November 2, 2022 at 11:13 am
Jonathan, I'm going to try your suggestion. I don't think it will work because it should be splited after ' - ' always and show me everything after that space dash space
Your example data does not include any hyphens. Please give us examples of what it really looks like.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
November 2, 2022 at 11:28 am
Hi guys,
I've just found the right solution based on Jonathan's idea
Restaurant , RIGHT(Restaurant, len(Restaurant) - charindex(' - ', Restaurant)-2) AS C
Thank you all so much, really
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply