Discussion Locked

Extract Data in SQL7.0

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a field in SQL table that houses a name (First, Last and Middle) I want a to write a query to extract this data into three seperate fields. I wrote the query and it works fine in MS Access 97/2000, but will not run in SQL. But there slite problem when it runs in Access, if there is no middle initial, it returns the middle initial value as #error.

SELECT
Left([BName],InStr(1,[BName]," ")-1) AS [First Name],
Trim(Mid([BName],InStr(1,[BName]," ")+1,InStr(InStr(1,[BName]," ")+1,[BName]," ")-InStr(1,[BName]," "))) AS [Middle Initial],
IIf(InStr(InStr([BName]," ")+1,[BName]," ")<>0, Right([BName],Len([BName])-InStr(InStr([BName]," ")+1,[BName]," ")),Right([BName],Len([BName])-InStr([BName]," "))) AS [Last Name]
FROM Student Name;

Thank you,

5 replies

by glen_mcleod

There's no InStr() function in SQL Server, you'll have to use CHARINDEX() instead.

Table names like the one you have here need to be enclosed in square brackets in SQL Server.

Glen

by anafiseh

The problem with CHARINDEX is I have you have to define the begining and end of the string. The string veries in length and may or may not have a middle initial. Additionaly, the function Mid is nor Trim are recognized in SQL. I'm also not sure if Iunderstood "Table names like the one you have here need to be enclosed in square brackets " I filed name "bname" is enclosed in brackets, the AS"" is the output, the only table name I have is Studet Name and it does not have to be enclosed in brackets...

This discussion is closed to new replies.