Solved

split string value in sql server data

Posted on 2009-05-10
5
348 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

738 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