April 25, 2013 at 2:26 am
Hi friends i have small doubt in sql server ,
table data contains like
id , name
1 , srinivas
2 , ravikumar
3 , jaipal
4 , ravisekhar
5 , ramugopal
6 , harikuma
based on this table data i want display 3rd positon charcter only
output like
id , name
1 , v
2 , m
3 , p
4 , h
5 , p
6 ,u
actualy i try query like select id,substring(name,3,1) from emp
but its not give exactely result.
plese tell me how to write query go get this result.
April 25, 2013 at 2:39 am
;with SCC_cTE(ID ,Name)
AS (SELECT 1, 'srinivas' union all
select 2, 'ravikumar' union all
select 3, 'jaipal' union all
select 4, 'ravisekhar' union all
select 5, 'ramugopal' union all
select 6, 'harikuma')
select id, name, SUBSTRING(REVERSE(name), 3, 1) as output
from SCC_cTE
---------------------------------------------------------
It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens[/url]
Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
April 25, 2013 at 2:42 am
substring reads from Left to Right, not Right to Left as some Languages from non English speaking countries might.
so when you put '3' in to your substring, your asking for the 3rd character from the left.
;with cte(id , name) as (
select 1 , 'srinivas'
union select 2 , 'ravikumar'
union select 3 , 'jaipal'
union select 4 , 'ravisekhar'
union select 5 , 'ramugopal'
union select 6 , 'harikuma'
)
select id,SUBSTRING(name,LEN(name) - 2,1) from cte
April 25, 2013 at 3:19 am
DECLARE @string varchar(10) = 'aapail'
SELECT SUBSTRING(REVERSE(@string),3,1)
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
April 25, 2013 at 3:34 am
hi,
before finding sub string you should trim the data otherwise it takes empty data also.
select id,SUBSTRING(Reverse(LTRIM(RTRIM(name))),3,1) from emp
--chalam
April 25, 2013 at 4:31 am
chalam87 (4/25/2013)
hi,before finding sub string you should trim the data otherwise it takes empty data also.
select id,SUBSTRING(Reverse(LTRIM(RTRIM(name))),3,1) from emp
;WITH Sampledata (id, name) AS (
SELECT 1 , 'srinivas ' UNION ALL
SELECT 2 , 'ravikumar ' UNION ALL
SELECT 3 , 'jaipal' UNION ALL
SELECT 4 , ' ravisekhar' UNION ALL
SELECT 5 , ' ramugopal' UNION ALL
SELECT 6 , ' harikuma ' )
-- LTRIM() isn't necessary:
SELECT
id,
name,
NameShowingPadding = '|' + name + '|',
Fails = SUBSTRING(REVERSE(name),3,1),
Oneway = SUBSTRING(REVERSE(RTRIM(name)),3,1),
Anotherway = LEFT(RIGHT(RTRIM(name),3),1)
FROM Sampledata
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
April 25, 2013 at 4:59 am
ChrisM@Work (4/25/2013)
chalam87 (4/25/2013)
hi,before finding sub string you should trim the data otherwise it takes empty data also.
select id,SUBSTRING(Reverse(LTRIM(RTRIM(name))),3,1) from emp
;WITH Sampledata (id, name) AS (
SELECT 1 , 'srinivas ' UNION ALL
SELECT 2 , 'ravikumar ' UNION ALL
SELECT 3 , 'jaipal' UNION ALL
SELECT 4 , ' ravisekhar' UNION ALL
SELECT 5 , ' ramugopal' UNION ALL
SELECT 6 , ' harikuma ' )
-- LTRIM() isn't necessary:
SELECT
id,
name,
NameShowingPadding = '|' + name + '|',
Fails = SUBSTRING(REVERSE(name),3,1),
Oneway = SUBSTRING(REVERSE(RTRIM(name)),3,1),
Anotherway = LEFT(RIGHT(RTRIM(name),3),1)
FROM Sampledata
Good one Chris ๐
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply