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


How to compare the first 20 chars onlys


How to compare the first 20 chars onlys

Author
Message
mattech06
mattech06
SSC-Enthusiastic
SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)

Group: General Forum Members
Points: 192 Visits: 236
Hi,

I have this code


SELECT
b.TransDate, b.TransType, b.SortCode, b.AccNum, b.TransDesc, b.Debit, b.Credit,
(ISNULL(b.Debit, 0) + ISNULL (b.Credit, 0)) as Amount,
ISNULL(r.OrthoID, 'Need ID') as OrthoID
FROM
EPSBankTransactions b
LEFT JOIN EPSBankRef r on b.TransDesc = r.BankID



but I would only like to try a join on the first 20 characters of b.TransDesc and not the full string. I'm not sure how I would best do that.

thanks
mattech06
mattech06
SSC-Enthusiastic
SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)

Group: General Forum Members
Points: 192 Visits: 236
sorted it out, didn't think it would work for some reason but

LEFT JOIN EPSBankRef r on SUBSTRING(b.TransDesc, 0, 21) = r.BankID

does the job.
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39152 Visits: 38521
Realize that you lose any benefits of having an index on the TransDesc column. If you can, you may want to consider adding a persisted computed column that stores the first 20 characters of the TransDesc column (LEFT(TransDesc,20)) and build an index on that column.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85517 Visits: 41081
Lynn Pettis (5/25/2014)
Realize that you lose any benefits of having an index on the TransDesc column. If you can, you may want to consider adding a persisted computed column that stores the first 20 characters of the TransDesc column (LEFT(TransDesc,20)) and build an index on that column.


+1000

But to add to Lynn's good suggestionn, an even better option would be to normalize the data instead of having more than one piece of information in a single column.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39152 Visits: 38521
Jeff Moden (5/25/2014)
Lynn Pettis (5/25/2014)
Realize that you lose any benefits of having an index on the TransDesc column. If you can, you may want to consider adding a persisted computed column that stores the first 20 characters of the TransDesc column (LEFT(TransDesc,20)) and build an index on that column.


+1000

But to add to Lynn's good suggestionn, an even better option would be to normalize the data instead of having more than one piece of information in a single column.


another +1000 here

Normalizing the data would be a very good thing.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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