Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to compare the first 20 chars onlys Expand / Collapse
Author
Message
Posted Sunday, May 25, 2014 7:32 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 6:01 AM
Points: 92, Visits: 148
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
Post #1574385
Posted Sunday, May 25, 2014 7:42 AM This worked for the OP Answer marked as solution
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 6:01 AM
Points: 92, Visits: 148
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.
Post #1574386
Posted Sunday, May 25, 2014 10:08 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:01 PM
Points: 23,062, Visits: 31,589
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.



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)
Post #1574398
Posted Sunday, May 25, 2014 10:12 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:42 PM
Points: 36,781, Visits: 31,237
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1574399
Posted Sunday, May 25, 2014 10:15 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:01 PM
Points: 23,062, Visits: 31,589
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.



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)
Post #1574401
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse