Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
T-SQL (SS2K8)
»
SUBSTRING Question?
SUBSTRING Question?
Rate Topic
Display Mode
Topic Options
Author
Message
dustin.walker
dustin.walker
Posted Monday, March 14, 2011 7:13 AM
Forum Newbie
Group: General Forum Members
Last Login: Monday, May 13, 2013 7:09 AM
Points: 2,
Visits: 207
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!
Post #1077681
Sean Lange
Sean Lange
Posted Monday, March 14, 2011 7:38 AM
SSCrazy Eights
Group: General Forum Members
Last Login: 2 days ago @ 8:46 AM
Points: 8,547,
Visits: 8,204
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 Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1077691
David Burrows
David Burrows
Posted Monday, March 14, 2011 7:39 AM
SSCertifiable
Group: General Forum Members
Last Login: 2 days ago @ 10:59 AM
Points: 6,350,
Visits: 5,355
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.
Post #1077694
David Burrows
David Burrows
Posted Monday, March 14, 2011 7:41 AM
SSCertifiable
Group: General Forum Members
Last Login: 2 days ago @ 10:59 AM
Points: 6,350,
Visits: 5,355
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.
Post #1077696
dustin.walker
dustin.walker
Posted Monday, March 14, 2011 7:49 AM
Forum Newbie
Group: General Forum Members
Last Login: Monday, May 13, 2013 7:09 AM
Points: 2,
Visits: 207
Thanks Sean! I was able to use datalength and it works successfully now. Thanks for everyone's help!
Post #1077703
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.