473,763 Members | 3,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Splitting a field by delimiter char(10)

6 New Member
*(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 7041
pbmods
5,821 Recognized Expert Expert
Heya, Iritchie. Welcome to TSDN!

Please use CODE tags when posting source code:

[CODE=MySQL]
MySQL code goes here.
[/CODE]
Sep 18 '07 #2
iritchie
6 New Member
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
1761
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
36225
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
2291
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 it seems that when I try to print that to the file, the program still prints the full CR+LF Windows-style ending. The code I have looks something like this: param_file<<"BSEARCH_ALG SIMPLE"<<( char )10; Is there any way to keep the program...
12
10090
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 it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
21
2304
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 sex = {"Male", "Female" };
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10140
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9935
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9819
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5268
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3916
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.