*(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 -
SELECT emplid, name,
-
CHARINDEX(char(10),address) first_cr,
-
-
CHARINDEX(char(10), address, CHARINDEX(char(10), address)+1) second_cr,
-
-
LEFT(address, charindex(char(10), address, -1)) as [address1],
-
-
SUBSTRING(address, CHARINDEX(char(10), address)+1, CHARINDEX(char(10), address, CHARINDEX(char(10), address)+1)-CHARINDEX(char(10), address)) as [address2],
-
-
RIGHT(address,LEN(address)-CHARINDEX(char(10),address,CHARINDEX(char(10),addr ess)+1 )) as [address3]
-
-
FROM empltable
-
...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: -
SELECT SUBSTRING(address, 1, NULLIF(CHARINDEX(char(10), address) - 1, -1)) AS [Address1],
-
SUBSTRING(address, CHARINDEX(char(10), address) + 1, LEN(address)) AS [Address2]
-
FROM empltable
-
...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!?
2 7010
Heya, Iritchie. Welcome to TSDN!
Please use CODE tags when posting source code:
[CODE=MySQL]
MySQL code goes here.
[/CODE]
Heya, Iritchie. Welcome to TSDN!
Please use CODE tags when posting source code:
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: -
SELECT emplid, name,
-
LEFT(address, NULLIF(CHARINDEX(char(10), address) - 1, -1)) AS [Address1],
-
RIGHT(address, ISNULL(NULLIF(CHARINDEX(char(10), REVERSE(address)) - 1, -1),
-
LEN(address))) AS [Address2]
-
FROM empltable
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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;
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |