473,395 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Splitting a field by delimiter char(10)

*(Apologies, I posted this in the SQL Server forum first)


Hello all,


I am trying to write a query which breaks down a single address field into individual fields, with char(10) or a carriage return as the delimiter.
"empltable" is the table I am using, and "address" is the field I am looking to split.

So far I have managed to:

Split the field into 3
Expand|Select|Wrap|Line Numbers
  1. SELECT emplid, name,
  2. CHARINDEX(char(10),address) first_cr,
  3.  
  4. CHARINDEX(char(10), address, CHARINDEX(char(10), address)+1) second_cr,
  5.  
  6. LEFT(address, charindex(char(10), address, -1)) as [address1],
  7.  
  8. SUBSTRING(address, CHARINDEX(char(10), address)+1, CHARINDEX(char(10), address, CHARINDEX(char(10), address)+1)-CHARINDEX(char(10), address)) as [address2],
  9.  
  10. RIGHT(address,LEN(address)-CHARINDEX(char(10),address,CHARINDEX(char(10),addr ess)+1 )) as [address3]
  11.  
  12. FROM empltable
  13.  


...but this only works on the test database, where I am inserting carriage returns into the field to allow the query to work. Basically, when there are less than 2 carriage returns in the address field, the query fails, as there is no error handling in the query.
So, on from that, I tried to introduce NULLIF into the query:
Expand|Select|Wrap|Line Numbers
  1. SELECT SUBSTRING(address, 1, NULLIF(CHARINDEX(char(10), address) - 1, -1)) AS [Address1],
  2. SUBSTRING(address, CHARINDEX(char(10), address) + 1, LEN(address)) AS [Address2]
  3. FROM empltable
  4.  

...This splits the address field into 2. I am having difficulty returning the middle section though without encountering the errors.
Ideally I would rewrite it to handle any number of returns!

Can someone please help!?
Sep 18 '07 #1
2 7010
pbmods
5,821 Expert 4TB
Heya, Iritchie. Welcome to TSDN!

Please use CODE tags when posting source code:

[CODE=MySQL]
MySQL code goes here.
[/CODE]
Sep 18 '07 #2
Heya, Iritchie. Welcome to TSDN!

Please use CODE tags when posting source code:

Expand|Select|Wrap|Line Numbers
  1. MySQL code goes here.
  2.  

Thanks! :)

Will use the code tags from now...

I also used the left and right reverse to get the first and last entries in the string, but again, struggle to come up with the results for the part in the middle:

Expand|Select|Wrap|Line Numbers
  1. SELECT emplid, name,
  2.     LEFT(address, NULLIF(CHARINDEX(char(10), address) - 1, -1)) AS [Address1],
  3.            RIGHT(address, ISNULL(NULLIF(CHARINDEX(char(10), REVERSE(address)) - 1, -1),
  4.              LEN(address))) AS [Address2]
  5. FROM empltable
  6.  
Sep 18 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: oembuilders | last post by:
Hi all, Would anyone know a way to convert a char(10) in format 'm/d/yyyy' to 'mm/dd/yyyy', so I can convert the column to datetime format. Thanks
13
by: Betaver | last post by:
What char represent "a new line"?13 or 10, or both? I got a lot of problem when read a char. Eg. ======== 1 2 a b ======== If I write: int n1,n2; char c1,c2;
4
by: jceddy | last post by:
Hey, I'm trying to write a file with unix-style newlines (ASCII character 10) from a c++ program on Windows...it seems that the most straightforward way to do that is just to print ( char )10, but...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
21
by: Wisdo | last post by:
Hi All, char to char ** is compile error. why? if i hava a string array. yes. it's not safe and it's better to use vector<stringinstead. but my point is the language feature. char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.