Solved

split string value in sql server data

Posted on 2009-05-10
5
347 Views
Last Modified: 2012-05-06
I am using SQL server 2005 database. In one of my table a field will have the values in the following format.
abcdef<ghijk><lmno>pqrs<tuvw> (or)
abc<defghij>klmnop (or)
abcdefghijkl<mnop>

now i want to write a query to select the first part that is, characters before the first '<'
how to do it?
0
Comment
Question by:siva_gk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 31

Expert Comment

by:James Murrell
ID: 24347501
0
 
LVL 1

Accepted Solution

by:
Mikhail Peterburgskiy earned 50 total points
ID: 24347543
declare @str as varchar(max)
select @str = 'abcdef<ghijk><lmno>pqrs<tuvw>'

select LEFT(@str, CHARINDEX('<', @str, 1) - 1)
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24347715
Use the following query for non-stored procedure approach:

SELECT LEFT(FieldName, CHARINDEX('<', FieldName, 1) - 1) AS ColumnName FROM TableName
0
 
LVL 1

Expert Comment

by:rbodepudi
ID: 24348089
It's easy, You can use a combination of functions to acheive this: First you have to find the position of first appearance of '<', then use that in LEFT() to get the desired result.
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24348354
well, all you want to locate the position of >, you can do it with quite a few different string function in SQL server. here are two function for you to learn. have a look at below example snippet.


declare @val varchar(max)
set @val='abcdefpqrs'

select LEFT(@val, CHARINDEX('<', @val, 1) - 1)

select substring(@val,1,CHARINDEX('<', @val, 1) - 1)
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question