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 |
|
wotrac
Yak Posting Veteran
98 Posts |
Posted - 2005-02-24 : 09:03:27
|
| I am trying to create a view that splitsan address field down to multiple fields.The address field may or may not contain up to 5 rows.I have made a start, but am having trouble where there is only the first line.select substring(cuaddress, 1, charindex(char(13), cuaddress,1)-1) AS ADDRESS1, substring(cuaddress, charindex(char(13), cuaddress,1)+1, len(substring(cuaddress, 1, charindex(char(13), cuaddress,1)+2))) AS ADDRESS2 from tablewhere code = 'ANIM03' |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-02-24 : 09:11:10
|
| Go for a udf likehttp://www.nigelrivett.net/SQLTsql/f_GetEntryDelimiitted.htmlthen you can return nulls from the udf if the entries don't exist==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
wotrac
Yak Posting Veteran
98 Posts |
Posted - 2005-02-24 : 09:26:53
|
| I don't wish to appear stupid, but.Where do I substitute my char(13) in your code and could you explainthe parameters passed to the function |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-02-24 : 10:54:44
|
| Is char(13) the delimitter? Thendeclare @s varcgar(1)select @s = char(13)select dbo.f_GetEntryDelimiitted(cuaddress, 1, @s, 'N') ,dbo.f_GetEntryDelimiitted(cuaddress, 2, @s, 'N') ,dbo.f_GetEntryDelimiitted(cuaddress, 3, @s, 'N') ,dbo.f_GetEntryDelimiitted(cuaddress, 4, @s, 'N')from tbl==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
wotrac
Yak Posting Veteran
98 Posts |
Posted - 2005-02-26 : 15:53:33
|
| I have modified your original code to accomodate the carriage returns, but I get an error when the address field is empty and I am trying to call the function more than once.The error readsInvalid length parameter passed to the substring functionI have included the code below. I have a vague idea what is going on, butam not too sure how to fix it.CREATE Function f_FormatAddress( @address VARCHAR(160), @line INT, @delim VARCHAR(1))Returns VARCHAR(100)asBEGINDeclare @i INT, @s VARCHAR(100), @pos INTSELECT @delim = char(13)SELECT @i = 1WHILE @line > 0BEGIN SELECT @line = @line - 1 select @pos = CHARINDEX(@delim,@address,@i) If @pos = 0 SELECT @pos = DATALENGTH(@address) + 1 If @line > 0 SELECT @i = @pos + LEN(@delim) ENDSELECT @s = SUBSTRING(@address,@i,@pos - @i)Return @sEND/**select add1 = dbo.F_FormatAddress (cuaddress,1,char(13)),add2 = dbo.F_FormatAddress (cuaddress,2,char(13)),add3 = dbo.F_FormatAddress (cuaddress,3,char(13)),add4 = dbo.F_FormatAddress (cuaddress,4,char(13)),add5 = dbo.F_FormatAddress (cuaddress,5,char(13)) from table where code = 'A'**/ |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-02-28 : 19:06:33
|
| if @pos > @i SELECT @s = SUBSTRING(@address,@i,@pos - @i)Return @s==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|
|