Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to Split last two char from varchar dataset..?

Author  Topic 

bhushan_juare
Starting Member

45 Posts

Posted - 2013-02-13 : 03:49:17
Hi All,
This is the query i have written but unable to extract last two char..

max(CASE CHARINDEX(' ', GT.FG_DESCRIPTION, 1) WHEN 0 THEN GT.FG_DESCRIPTION
ELSE SUBSTRING(GT.FG_DESCRIPTION, 1, CHARINDEX(' ', GT.FG_DESCRIPTION, 1) - 2) END) AS PROD_TYPE

Output coming
A B
14.00-24 32 PR BKT EM937 TL 14.00-2
700/40-22.5 16 PR BKT FLOT648 TL 700/40-22.
30.5L-32 12 PR BKT TR137 TR 30.5L-3

Required Output
A B
14.00-24 32 PR BKT EM937 TL TL
700/40-22.5 16 PR BKT FLOT648 TL TL
30.5L-32 12 PR BKT TR137 TR TR

Thanks
Bhushan

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-13 : 04:56:02
Whats A and B? cant make out from query as it specifies only single column

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2013-02-13 : 12:01:42
If you know it is the last two characters then what about the RIGHT function? Since you used the CHARINDEX function I think you might look at the REVERSE function.

djj
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2013-02-13 : 15:13:16
I think he wants the last 2 alphas when there are 2 alphas in a row

declare @str varchar(50)='30.5L-32 12 PR BKT TR137 TR 30.5L-3'

select
SUBSTRING(@str,LEN(@str)-patindex('%[A-Z][A-Z]%',REVERSE(@str)),2)

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -