Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 10th, 2003, 12:54 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL question

I am using Access project file to connect to SQL Server 2000.

In my query I want to display First_name and Last_name from a Name field. I do this by copying left and right of a ' ' (space) to separate first and last name:

SELECT LEFT(Name,CHARINDEX(' ',Name)-1) AS FNAME, RIGHT(Name,LEN(Name)-CHARINDEX(' ',Name)) AS LNAME
FROM tblClients
...etc...

On occasion I get an error (I think) when a Name does not contain a ' ' (space) character.

What I'd like to do is first compare (using something like IIF function) if there is a ' ' in the string:

SELECT IIf(CHARINDEX(' ',Name) > 0), LEFT(Name,CHARINDEX(' ',Name)-1), Name) AS FNAME, IIf(CHARINDEX(' ',Name) > 0), RIGHT(Name,LEN(Name)-CHARINDEX(' ',Name)),Name) AS LNAME
FROM tblClients

So my question: Is there a good way to do this? Seems IIF is not recognized as a function name in Access projects.



 
Old October 10th, 2003, 01:08 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this:

_____________________________

SELECT
CASE
     WHEN CHARINDEX(' ',Name) > 0)
          THEN
              LEFT(Name,CHARINDEX(' ',Name)-1)
     ELSE Name
END
AS FNAME,
CASE
     WHEN CHARINDEX(' ',Name) > 0)
         THEN
             RIGHT(Name,LEN(Name)-CHARINDEX(' ',Name))
         ELSE Name
END
AS LNAME,
FROM tblClients
___________________

This uses the CASE logic. Look it up in Books On Line. It can be a powerful statement to use.



Sal
 
Old October 10th, 2003, 01:45 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your quick reply! That's a useful bit to keep in my SQL bag of tricks.

The SQL editor is accepting the syntax. But I think I still have a logic error. The error message is:

"Invalid length parameter passed to the Substring function."

Which is the original error I got and what prompted me to put in the extra logic...

I'm thinking maybe it is if the string is NULL or "". (???)

Is there a way to test additional conditions? Such as:

WHEN CHARINDEX(' ',Name) > 0) OR (Name = NULL)

Please excuse my ignorance... I was planning on creating a scalar text function but couldn't get past the syntax.

 
Old October 10th, 2003, 02:00 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I figured it out...

The error condition is when the Name is " " (one character string that is a space) which happens frequently in databases, due to human (lamer) error.

Anyways... Thanks again for your help.






Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Question psnow1985 ASP.NET 2.0 Basics 23 March 21st, 2008 11:48 AM
sql query question ldp101068 SQL Server 2000 6 December 3rd, 2007 03:41 PM
ASP / Sql question joebeem Classic ASP Basics 0 August 28th, 2007 02:45 PM
A SQL question pankaj_daga Access 5 December 5th, 2005 04:37 PM
Sql-question boson SQL Language 2 July 3rd, 2004 06:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.