Solved

SQL server 2000 - Calculated column - how do i return the first word of a field???

Posted on 2004-09-20
3
454 Views
Last Modified: 2008-03-04
Hi I am trying to create a column in a table that always returns the first part of another field. I need to strip a postcode down to the first part ie "L35 6YT" would be "L35" the first part is between 2 and 4 charactors long so I cant use a simple trimming function. I have done it in access using this code

 Left([IntroducerPostcode],InStr([IntroducerPostcode] & " "," ")-1)

but SQL server 2000 doesnt read the commands??

any ideas? Im doing it so I can compare the postcode in a transactions table to the postcode in another table, but the postcodes stored in the other table are only the first part and not the full code that is stored in the transactions table. Im thinking maybe I can set a join up between the two fields in a query once I can get only the first part to return in the tranactions table.

Hope that makes sense! There may be an easier way to do it in which case let me know!

Cheers!
0
Comment
Question by:goldsmithwilliams
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
solution46 earned 125 total points
ID: 12101616
use charindex instead of InStr

From the SQL Server books online...

CHARINDEX ( expression1 , expression2 [ , start_location ] )

Arguments
expression1

Is an expression containing the sequence of characters to be found. expression1 is an expression of the short character data type category.

expression2

Is an expression, usually a column searched for the specified sequence. expression2 is of the character string data type category.

start_location

Is the character position to start searching for expression1 in expression2. If start_location is not given, is a negative number, or is zero, the search starts at the beginning of expression2.
0
 

Author Comment

by:goldsmithwilliams
ID: 12101706
Thanks,

Im new to SQL so am struggling with this, what should I be putting into the formula box by looking at my access code?

Cheers again!
0
 
LVL 9

Expert Comment

by:solution46
ID: 12102288
Er, your best bet is probably to make a view in SQL Server, using the above SQL, and link to that in Access; then use the usual access notation to use your query in your forms.

If you need any more guidance, please give me a few details of your setup and I'll see what I can do.

s46.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now