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 2005 Forums
 Transact-SQL (2005)
 Problem parsing character string

Author  Topic 

donchamp
Starting Member

9 Posts

Posted - 2010-07-06 : 21:19:32
0



I need to parse a variable lenght character string with 1 - 10 characters and save the last character to a table. I've tried using substring but get a syntax error with the following code.

Declare @z varchar (10) set @z = ' ' select substring(SortCode, 1,1) as @z
from TempTable

Kindly let someone let me know what's missing.

Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-07-06 : 22:05:39
how many rows you have in the TempTable ? By assignning it the result to a variable, you are effectively only getting one record out of it only.

if you want the last character, why are you using subtring(SortCode, 1, 1) that is the first character from left.

should be

select right(SortCode, 1)
from TempTable



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

donchamp
Starting Member

9 Posts

Posted - 2010-07-06 : 22:29:36
I have 64 rows in the table and sortcode in each row varies in length from 1 to 10 characters I need to extract the characters individually so the start parameter in the substring statement can vary from 1 to 10.
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-07-07 : 03:16:47
If this ( select right(SortCode, 1) from TempTable ) is not working you should post some sample data and your expected output.

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page
   

- Advertisement -