Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
bubberz
Constraint Violating Yak Guru
289 Posts |
Posted - 2008-05-13 : 13:16:19
|
Hello!We are on SqlServer 2005.Let me point out at the beginning that I don't have anyway to normalize this structure or get the admins to change the way the data is stored. We don't own the database where this is housed...we're just given the information via an .xls file...which we import to a SQLServer table.I have some data that is given to me that has two columns (below is for an example):Column A is an identifiying number, i.e. for a projectColumn B is a comma separated list of account strings for the projectA sample layout of what we get via the .xls file might look like the following (Column A is to the left of the dashes, and Column B is to the right of the dashes):AA.ProjectBuildTower ----- 2222, 3333, 4444, 5555BB.ProjectBuildFence ----- X900, 6789, 9000, 9876What I need to do is now haveprojects listed out in Column A with each of it's account strings in Column B like so:AA.ProjectBuildTower ----- 2222AA.ProjectBuildTower ----- 3333AA.ProjectBuildTower ----- 4444AA.ProjectBuildTower ----- 5555BB.ProjectBuildFence ----- X900BB.ProjectBuildFence ----- 6789BB.ProjectBuildFence ----- 9000BB.ProjectBuildFence ----- 9876Any suggestions would greatly help!Thanks! |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
bubberz
Constraint Violating Yak Guru
289 Posts |
Posted - 2008-05-13 : 14:12:05
|
I'm trying to use this example:--*****************--*****************USE [ReportingDB]GO/****** Object: UserDefinedFunction [dbo].[fSplit] Script Date: 05/13/2008 12:03:15 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE FUNCTION [dbo].[fSplit]( @List VARCHAR(6000), @SplitOn VARCHAR(5)) RETURNS @RtnValue TABLE( ID INT identity(1,1), Value VARCHAR(100)) AS BEGIN WHILE (Charindex(@SplitOn,@List)>0) BEGIN INSERT INTO @RtnValue (value) SELECT Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1))) SET @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List)) END INSERT INTO @RtnValue (Value) SELECT Value = ltrim(rtrim(@List)) RETURNEND--***************--***************I've added it to my UDFs under that database under table valued Functions, but when I run the following SQL:SELECT colA, dbo.fSplit([Program Code], ',') AS Expr1FROM mainTable...I get:Can't find column dbo, or user defined funciton or aggregate fSplit, or the name is ambiguous.I'm sure it's something goofy I'm doing! |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-13 : 14:13:41
|
fSplit is a table-valued function, yet you are calling it like a scalar-valued function.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2008-05-13 : 14:29:01
|
SELECT mainTable.colA, split.ValueFROM mainTableCROSS APPLY dbo.fSplit(mainTable.[Program Code], ',') split- Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
bubberz
Constraint Violating Yak Guru
289 Posts |
Posted - 2008-05-14 : 12:08:21
|
All, this did the trick ...and thanks for you help!Declare @Temp Table(ColumnA VarChar(40), ColumnB VarChar(400))insert into @Temp Values('AA.ProjectBuildTower','2222, 3333, 4444, 5555')insert into @Temp Values('BB.ProjectBuildFence','X900, 6789, 9000, 9876')Declare @Output Table(Header VarChar(40), Data VarChar(20))While Exists(Select * From @Temp Where CharIndex(',', ColumnB) > 0) Begin insert into @output(Header, Data) Select ColumnA, Left(ColumnB, CharIndex(',', ColumnB)-1) From @Temp Where CharIndex(',', ColumnB) > 0 Update @Temp Set ColumnB = LTrim(Right(ColumnB, Len(ColumnB)-CharIndex(',', ColumnB))) EndInsert Into @Output(Header, Data) Select ColumnA, ColumnBFrom @TempSelect * From @Output Order By Header, Data |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-14 : 12:49:47
|
Not sure why you'd want to do it that way, but okay.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
|
|
|
|