Question : How to deconcatenate a field that has tmultiple ASCII Char(13) int the string

Looking at a SQL DB field the data is 1big streetXsomeplaceXsomestateXzip. Where x=the unicode character that is shaped like a square. I want to query this data to produce 4 separate data fields.
The logic would be this,but I cant see how to return the potions of the X character
select substring(fieldname,1, position of first occurance of x) as ADDRESS1
select substring(fieldname,position of first occurance of x, position of second occurance of x) as ADDRESS2 etcUsing SelectSUBSTRING(MYCOL, 1,PATINDEX('%',+CHAR(13)+'%',MYCOL) ASADDR1 returns the correct set of characters to ADDR!,now I want toextract the next set inthe string to ADDR2, this string may / may notexist.

Answer : How to deconcatenate a field that has tmultiple ASCII Char(13) int the string

Hi, have seen this hanging around, so either no longer a problem, or still waiting for a fix. In case it is the latter, I have prepared a user defined function for you, with some examples on how to use it...

Please let me know what you think, or how you got on...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
-- two arguments, the first is the string or column to unpack, the second is the delimiter
select * from dbo.udf_unpack_address('streetXsomeplaceXsomestateXzip','X')
 
-- can easily handle CR
select * from dbo.udf_unpack_address('street'+char(13)+'someplace'+char(13)+'somestate'+char(13)+'zip',CHAR(13))
 
-- can combine with other string utilities
select * from dbo.udf_unpack_address(replace('street'+char(13)+'someplace'+char(13)+'somestate'+char(13)+'zip',char(13),char(9)),CHAR(9))
 
 
CREATE FUNCTION udf_unpack_address (@unstring varchar(2000), @delimiter varchar(1))
-- two arguments, the first is the string or column to unpack, the second is the delimiter
RETURNS @address table 
          (street varchar(100), 
           suburb varchar(100), 
           state varchar(100), 
           zip varchar(100))
AS
 
BEGIN
---Tab = char(9)
---Line feed = char(10)
---Carriage return = char(13)
   DECLARE @startingposition    int,
           @delimiterposition   int,
           @unpackelement       int
 
   DECLARE @street              varchar(100),
           @suburb              varchar(100),
           @state               varchar(100),
           @zip                 varchar(100)
 
-- SET @delimiter = CHAR(13)
   SET @startingposition = 0
   SET @delimiterposition = 1
   SET @unpackelement = 1
   SET @unstring = @unstring+@delimiter
 
   WHILE @delimiterposition > 0
   BEGIN
      SET @delimiterposition = charindex(@delimiter, @unstring, @startingposition + 1)
      IF  @delimiterposition > 0 
      BEGIN
          IF  @unpackelement < 2 set @street = substring(@unstring, @startingposition + 1, (@delimiterposition - @startingposition - 1))
          IF  @unpackelement = 2 set @suburb = substring(@unstring, @startingposition + 1, (@delimiterposition - @startingposition - 1))
          IF  @unpackelement = 3 set @state  = substring(@unstring, @startingposition + 1, (@delimiterposition - @startingposition - 1))
          IF  @unpackelement > 3 set @zip    = substring(@unstring, @startingposition + 1, (@delimiterposition - @startingposition - 1))
          SET @unpackelement = @unpackelement + 1
      END
      SET @startingposition = @delimiterposition
   END
 
   INSERT @address values (@street,@suburb,@state,@zip)
 
   RETURN
 
END
Open in New Window Select All
Random Solutions  
 
programming4us programming4us