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 2000 Forums
 Transact-SQL (2000)
 truncating a string

Author  Topic 

simonchia
Starting Member

11 Posts

Posted - 2004-06-16 : 05:46:38

greeting,

I need to get the last value of this string value XXXX.99999.01 which is 01, can anyone help me in this with t-sql? 01 is 2 digit, the last digit might grow from 2 to 4.
Thanks in advance for your help.


spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-06-16 : 05:51:05
will this do?
select substring(valueField, len(valueField)-1, 2)


Go with the flow & have fun! Else fight the flow :)
Go to Top of Page

drymchaser
Aged Yak Warrior

552 Posts

Posted - 2004-06-16 : 08:57:48
This article may help you. It involves breaking up delimited strings using a function called PARSENAME(). This will work with four-part strings.

http://www.sqlteam.com/item.asp?ItemID=15044

select parsename('XXXX.99999.01 ', 1) 'results'
results
-------
01
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2004-06-16 : 09:01:22
if its only ever going to be 2 digits, why not just do:-

select right('XXXX.99999.01',2)
Go to Top of Page

drymchaser
Aged Yak Warrior

552 Posts

Posted - 2004-06-16 : 09:04:55
quote:
Originally posted by RickD

if its only ever going to be 2 digits, why not just do:-

select right('XXXX.99999.01',2)



It isn't...
quote:
, the last digit might grow from 2 to 4.
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-06-16 : 09:06:34
Double Post:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=36336
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-06-16 : 09:13:34
try this:

Declare @myVal nvarchar(100)

Set @myVal = 'xxxxxx.999999.01'

Select reverse(left(reverse(@myVal),charindex('.',reverse(@myVal))-1))

Set @myVal = 'xxxxxx.999999.1234'

Select reverse(left(reverse(@myVal),charindex('.',reverse(@myVal))-1))

Set @myVal = 'xxxxxx.999999.65432157'

Select reverse(left(reverse(@myVal),charindex('.',reverse(@myVal))-1))

Corey
Go to Top of Page
   

- Advertisement -