Solved

Reterieve the values in T-SQl

Posted on 2013-11-26
2
320 Views
Last Modified: 2013-11-26
Hi Team,

I want to achieve the below result in T-SQL
ex:

@t='Country|Aug|BA.2|1|N/A'

I want to replace BA.2 dynamically, that's means any values after 2nd "|" can be replaced by my another variables.
0
Comment
Question by:prashant04
[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
2 Comments
 
LVL 37

Accepted Solution

by:
ValentinoV earned 500 total points
ID: 39677712
Something like this?

declare @t varchar(1000) = 'Country|Aug|BA.2|1|N/A';
declare @replacementValue varchar(1000) = 'something new';

with PipePosition as (
	select CHARINDEX('|', @t, CHARINDEX('|', @t)+1) PositionOfSecondPipe
		, CHARINDEX('|', @t, CHARINDEX('|', @t, CHARINDEX('|', @t)+1)+1) PositionOfThirdPipe
)
select REPLACE(@t
	, SUBSTRING(@t, PositionOfSecondPipe+1, PositionOfThirdPipe - PositionOfSecondPipe-1)
	, @replacementValue)
from PipePosition

Open in new window

0
 

Author Comment

by:prashant04
ID: 39677787
Thanks for the solution its working!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql2016-WIn10: standard,for SQL servc-account.. 51 55
If in a where clause in t-sql 7 53
SQL syntax question 6 73
When to use a Unique Index? A Clustered Index? 5 76
A recent question popped up and the discussion heated up regarding updating a COMMENTS (TXT) field in a table using SSRS. http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/MS-SQL_Reporting/Q_27475269.html?cid=1572#a37227028 (htt…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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