473,836 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Split a column into 2 columns

Hi everyone
I guess this should be a simple question for the gurus
I have a Data in a column which is to be places in 2 columns instead of
one. How do i go about doing it in MS SQL server? Could someone please
help me. I could do it in access with an update query but things are a
little different in SQL server so I am a little lost.

Eg.
Name
John?Doe
to be split into
Name LastName
John Doe

Thanks in advance.
Prit

Jul 23 '05 #1
6 21893

"Prit" <pr************ ****@hotmail.co m> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hi everyone
I guess this should be a simple question for the gurus
I have a Data in a column which is to be places in 2 columns instead of
one. How do i go about doing it in MS SQL server? Could someone please
help me. I could do it in access with an update query but things are a
little different in SQL server so I am a little lost.

Eg.
Name
John?Doe
to be split into
Name LastName
John Doe

Thanks in advance.
Prit


You can use string functions:

declare @name varchar(20)
set @name = 'john?doe'

select left(@name, charindex('?', @name)-1), right(@name,
len(@name)-charindex('?', @name))

So something like this?

insert into dbo.Destination (fname, lname)
select left(fullname, charindex('?', fullname)-1), right(fullname,
len(fullname)-charindex('?', fullname))
from dbo.Source

Simon
Jul 23 '05 #2
Simon
Thanks for the reply. It does work with the insert query. When I tried
to use it with an update query it did not quite like it. I wasn't sure
what was going wrong. Sorry I am still trying to find my way around the
SQL server. Does it have a update query equivalent of MS access?
thanks
prit

Jul 23 '05 #3
On 13 Jan 2005 12:52:27 -0800, Prit wrote:
Simon
Thanks for the reply. It does work with the insert query. When I tried
to use it with an update query it did not quite like it. I wasn't sure
what was going wrong. Sorry I am still trying to find my way around the
SQL server. Does it have a update query equivalent of MS access?
thanks
prit


Hi Prit,

This UPDATE should work just as well:

UPDATE MyTable
SET fname = left(fullname, charindex('?', fullname)-1),
lname = right(fullname, len(fullname)-charindex('?', fullname))

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #4
Hi Hugo
Thanks for the reply
It gives me the following error

Server: Msg 8114, Level 16, State 5, Procedure Part_Trigger, Line 26
Error converting data type varchar to numeric.

Surprisingly enough both my variables are varchar (50)
Any ideas why this is happening?

Jul 23 '05 #5
On 13 Jan 2005 14:50:44 -0800, Prit wrote:
Hi Hugo
Thanks for the reply
It gives me the following error

Server: Msg 8114, Level 16, State 5, Procedure Part_Trigger, Line 26
Error converting data type varchar to numeric.

Surprisingly enough both my variables are varchar (50)
Any ideas why this is happening?


Hi Prit,

The error message claims to stem from a procedure called "Part_Trigg er".
Unless you have a very funny naming scheme, this procedure is actually a
trigger, and it's probably a trigger that fires on updates to the table
you are handling.

I'd suggest you find the code for the trigger and check it for the cause
of this error. Start looking near line 26 (see error message). Post the
trigger's code here, along with the CREATE TABLE statement for the table,
if you can't find the error.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #6
Thanks hugo
Got it to work. There was a problem with the trigger. It was updating a
table which did not have the updated design of this table. I updated
the table and it worked like a charm. :)
Prit

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13250
by: Tim | last post by:
Hi I want to be able to split the contents of a text field into two or maybe three columns. The text field contains text AND HTML mark-up. My initial thought was to find the middle character and then go to the nearest space and split the text that way, but it sometimes splits in the middle of an HTML tag: not pretty! My next idea was to strip the HTML tags from the text and then split it up
2
12845
by: SL_McManus | last post by:
Hi All; I am fairly new to Perl. I have a file with close to 3000 lines that I would like to split out in a certain way. I would like to put the record type starting in column 1 for 2 spaces, the employer code in column 23 for 29 spaces and employer description in column 53 for 30 spaces. I have tried modifying an existing file with no real success. I haven't found anything that specifically answers my question. Any guidance would be...
3
11525
by: Stephen Matthews | last post by:
Help please i have a file which im importing, however it is a single column, the data looks like C8517673505 N7062175 C8517673516 N7062178 C8517673527
5
4267
by: Dave | last post by:
Is there a way to divide a datagrid into "newspaper" style columns so that column 1 would have rows 1-10, column two would have rows 11-20 etc? Thanks, Dave.
1
4529
by: heckstein | last post by:
I am working in Access 2002 and trying to create a report from our company's learming management system. I am not a DBA and most of my SQL knowledge has been self taught through trial and error. I have created an access query to track the number of training hours for a training group. The query is working except for one piece of data and I hoping someone can help me. There is a field titled enrollment status, which presents an alpha character of...
3
4875
by: ashok | last post by:
Hi, I need a function that will divide text from mysql in 2 parts, so that I can display first half in one column and second half in second column. I can't find what function will do this job. Here is part of what I have. $myrow = mysql_fetch_assoc($rsindexpage); //function wordcount $wc = str_word_count($myrow); $half = ($wc/2);
2
5088
by: Dscar | last post by:
Hi, I am a beginner in ACCESS, I've imported data into access, and then realized that I need to split the information in one of my columns into 2 columns. the information looks like this: 00700400000000 00700400001230 00700400102000 I would like 007004 in one column and the rest of the info in another column. I know in excel I could do text to columns, however the is 20,000 records in access and can not export to excel and split the info. ...
4
2568
by: Gilberto | last post by:
Hello, I have a couple of forms using the code to FIND AS YOU TYPE from Allen Browne (http://allenbrowne.com/AppFindAsUType.html). It worked PERFECTLY until yesterday when i splitted the db into FE/BE. The tables link ok and everything works ok EXCEPT this function. When i open the form it gives me a underlining line (iReturn = ctl.Parent.PageIndex IN THE PARENTNUMBER function in BOLD) and indicating that iReturn=0. I am new with access and...
5
2574
by: chinni0719 | last post by:
Hi I need to split the string which looks like n.col1 , b.col1 , n.col2 , b.col2, n.col3 , b.col3 , b.col4 , n.col4 , b.col5,n.col5 n.col1,n.col2 are columns these are present in @columns select @columns gives this string the length of @column is not fixed........we may have n number of columns seperated by ',' but we need to display only first 6 out of them
0
9816
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
9668
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10840
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...
0
10546
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10254
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
6978
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
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...
1
4448
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
3112
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.