SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SUBSTRING Question?


SUBSTRING Question?

Author
Message
dustin.walker
dustin.walker
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 290
Can someone please help! I'm having an issue with a query. It's using a SUBSTRING but I think there's a problem with it. It returns 0 rows when it should return 1. Here is the query in question.

SELECT v.veh_id, vi.inventory_id, SUBSTRING(bd.buft_string_1, 35, 70), bd.*
FROM buft_chrysler_data AS bd
INNER JOIN vehicle AS v WITH (NOLOCK)
ON SUBSTRING(bd.buft_string_1, 14, 17) = v.vin
INNER JOIN vehicle_inventory AS vi WITH (NOLOCK)
ON v.veh_id = vi.veh_id
AND vi.own_dtm = (SELECT MAX(o.own_dtm)
FROM ownership AS o WITH (NOLOCK)
WHERE o.veh_id = v.veh_id
AND o.tran_account_id = 1) -- Chrysler
INNER JOIN message_dcx_met_part_audit AS mdmpa WITH (NOLOCK)
--ON LTRIM(RTRIM(SUBSTRING(bd.buft_string_1, 35, 70))) = LTRIM(RTRIM(mdmpa.message_text))
ON SUBSTRING(bd.buft_string_1, 35, 70) = mdmpa.message_text
WHERE bd.tran_event_id = 62139

I think the issue is on "SUBSTRING(bd.buft_string_1, 35, 70) = mdmpa.message_text". The mdmpa.message_text field is a varchar(70), this is why we're using 70 as our Substring length. However, it doesn't seem to like this. It should match where message_text is equal to "ALL MET ITEMS FOR VIN PROCESSED SUCCESSFULLY". If I simply use "SUBSTRING(bd.buft_string_1, 35, 44) instead where 44 is the exact length of the string it works just fine. How do I account for a varchar field though?

Any help would be greatly appreciated!
Sean Lange
Sean Lange
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60605 Visits: 17954
Try using datalength instead of a hardcoded length. Think that should get you the desired result.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
David Burrows
David Burrows
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16307 Visits: 10109
I think the issue is on "SUBSTRING(bd.buft_string_1, 35, 70) = mdmpa.message_text"


Did you prove this by removing the join and getting rows returned?

If it is the substring then the most likely cause is either bd.buft_string_1 contains non space chars (hidden or otherwise) after "ALL MET ITEMS FOR VIN PROCESSED SUCCESSFULLY" or the text does not start at column 35.

What is the size of bd.buft_string_1?


Far away is close at hand in the images of elsewhere.

Anon.


David Burrows
David Burrows
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16307 Visits: 10109
Also check that mdmpa.message_text does not have non space chars following "ALL MET ITEMS FOR VIN PROCESSED SUCCESSFULLY"


Far away is close at hand in the images of elsewhere.

Anon.


dustin.walker
dustin.walker
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 290
Thanks Sean! I was able to use datalength and it works successfully now. Thanks for everyone's help!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search