Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
id  name

1   Ajil S (C1185)
2   Ajil S (C1185)
3   Joe P (C1157)
4   Joe(A1160)
5   Raj (A1174)
6   Sreemathi (A1190)
7   Nadarajan P (C1187)
8   Sreemathi S (C1230)
9   Rajakumaran C (C1192)

SQL




i have a name like this "name"(5 degits of code)

how to get the name from this column,this is my query

SQL
select left(name,(len(name)-7)) from tbl_payment_fake


it shows the error
ASM
Msg 537, Level 16, State 5, Line 1
Invalid length parameter passed to the LEFT or SUBSTRING function.
Posted
Updated 18-Dec-13 2:02am
v3
Comments
Sergey Alexandrovich Kryukov 18-Dec-13 8:49am    
The question is: why did you mix up names with other information in one column? You need to design database properly, instead of solving such problems.
—SA

Chances are that you have bad data in there: try running a simple query:
SQL
SELECT id, name FROM tbl_payment_fake WHERE LEN(Name) < 8
and see what you get - if you have any you may need to fix them first.
 
Share this answer
 
Comments
King Fisher 18-Dec-13 23:18pm    
thanks ,i solved this
Test it:
SQL
DECLARE @tmp TABLE ([Name] VARCHAR(30))

INSERT INTO @tmp ([Name])
VALUES('Ajil S (C1185)'),
('Ajil S (C1185)'),
('Joe P (C1157)'),
('Joe(A1160)'),
('Raj (A1174)'),
('Sreemathi (A1190)'),
('Nadarajan P (C1187)'),
('Sreemathi S (C1230)'),
('Rajakumaran C (C1192)')


SELECT RTRIM(LEFT([Name],CHARINDEX('(',[Name])-1)) AS NewName
FROM @tmp


Result:
Ajil S 
Ajil S 
Joe P 
Joe
Raj 
Sreemathi 
Nadarajan P 
Sreemathi S 
Rajakumaran C 
 
Share this answer
 
v2
SQL
select reverse(substring(reverse(name),8,100)) from tbl_payment_fake




its working.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900