Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

split string value in sql server data

Posted on 2009-05-10
5
Medium Priority
?
352 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 200 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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

721 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