![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I'm trying to create a function that splits up a column by spaces, and I thought creating a function that finds the spaces with CHARINDEX and then SUBSTRING on those values would an approach. I get an error saying that the I have an Invalid column 'Course_Number'. Not sure why but I am very new to User Defined Functions. Here is what I have so far: |
|
SET @Ind1 = CHARINDEX(' ', Course_Number, 4) SET @Ind2 = CHARINDEX(' ',Course_Number, (CHARINDEX(' ', Course_Number, 4)+1)) |
#3
| |||
| |||
|
|
jknaty wrote: I'm trying to create a function that splits up a column by spaces, and I thought creating a function that finds the spaces with CHARINDEX and then SUBSTRING on those values would an approach. I get an error saying that the I have an Invalid column 'Course_Number'. Not sure why but I am very new to User Defined Functions. Here is what I have so far: Please don't multi-post (post to each of several newsgroups separately), but rather cross-post (post to all of them in a single go). But only do that if it's truly relevant to all of them. SET @Ind1 = CHARINDEX(' ', Course_Number, 4) SET @Ind2 = CHARINDEX(' ',Course_Number, (CHARINDEX(' ', Course_Number, 4)+1)) This is your problem. See microsoft.public.sqlserver for details. |
#4
| |||
| |||
|
|
On May 23, 3:22 pm, Ed Murphy <emurph... (AT) socal (DOT) rr.com> wrote: jknaty wrote: SET @Ind1 = CHARINDEX(' ', Course_Number, 4) SET @Ind2 = CHARINDEX(' ',Course_Number, (CHARINDEX(' ', Course_Number, 4)+1)) This is your problem. See microsoft.public.sqlserver for details. I understand, amd I'm new to posting to groups and just really wanted to be sure this got posted to the right group of people to help me out. I'm still not sure what is wrong but what I did to make things a bit more clean is break out the part that I really want into a separate function: |
|
CREATE FUNCTION CourseEvalBreakdown ( @Course_Number char(20) ) |
|
This works and I get the one column that I pass in to the seperated columns I'm looking for, but now I'm stuck trying to figure out how to merge those columns into the row that I got the original column which was passed to this function. I tried to create a select statement where I call this function but I got stuck there. Then I thought maybe I can create a function to create a new table with everything I'm looking for calling this function where I need it and build the table I'm looking for but again I'm stuck. Any ideas on either? |
#5
| |||
| |||
|
|
What you really want is the following: CREATE FUNCTION CourseEvalBreakdown ( @fskey int ) RETURNS @CourseTable TABLE ( Col CHAR(2), Area CHAR(4), Number CHAR(4), Section CHAR(4), Term CHAR(3) ) AS BEGIN DECLARE #CourseTableTemp TABLE ( FStaffKey int , Course_Number char(20) , Ind1 int , Ind2 int , Term char(3) , Col char(2) , Area char(4) , Number char(4) , Section char(3) ) INSERT INTO #CourseTableTemp (FStaffKey, Course_Number, Term) SELECT FStaffKey, Course_Number, Term FROM Eval WHERE FStaffKey = @fskey UPDATE #CourseTableTemp SET Ind1 = CHARINDEX(' ', Course_Number, 4) UPDATE #CourseTableTemp SET Ind2 = CHARINDEX(' ', Course_Number, Ind1 + 1) UPDATE #CourseTableTemp SET Col = SUBSTRING(Course_Number, 1, 2) , Area = UPPER(SUBSTRING(Course_Number, 4, @Ind1-4)) , Number = UPPER(SUBSTRING(Course_Number, @Ind1+1, @Ind2-@Ind1-1)) , Section = SUBSTRING(Course_Number, @Ind2+1, 3) INSERT INTO @CourseTable SELECT Col, Area, Number, Section, Term FROM #CourseTableTemp RETURN END GO |
#6
| |||
| |||
|
|
This is great, and looks like what I'm looking for. But I tried it and I keep getting "Cannot access temporary tables from within a function". Should I create the table first and then drop it when the function is done? |
#7
| |||
| |||
|
|
This is great, and looks like what I'm looking for. But I tried it and I keep getting "Cannot access temporary tables from within a function". Should I create the table first and then drop it when the function is done? |
|
DECLARE #CourseTableTemp TABLE ( |
|
DECLARE @CourseTableTemp TABLE ( |
#8
| |||
| |||
|
|
Just change: DECLARE #CourseTableTemp TABLE ( To DECLARE @CourseTableTemp TABLE ( And use @CourseTableTemp throughout the function. |
|
Logically, temp tables and table variables are very similar, but there is some fine-print which can be really confusing. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
![]() |
| Thread Tools | |
| Display Modes | |
| |