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
 General SQL Server Forums
 New to SQL Server Programming
 coding for a substring

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2011-11-23 : 09:50:17
I have a varchar(50) field 'Descript' that a case expression was written for to do a data conversion. If the data that was intended for that field was more than 50 chars long, the data that would go into 'descript' was "SEE NOTES FOR DESCRIPTION", and the data that was intended for there would be concatenated to other data (as the end of the string) in the 'notes' column. now, what the customer decides they want is, in the event of a string longer than 50 chars being designated for the 'Descript' field, they want those first 50 chars IN that field, and the full string, as it is currently, in the 'notes' field. So my question is, how do i code to pull the substring from that 'notes' field and put the appropriate 50 chars back in the 'descipt' column and replace the "SEE NOTES FOR DESCRIPTION" line that is in there now?

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-11-23 : 09:53:42
update tbl
set descript = left(Notes,50) where descript = 'SEE NOTES FOR DESCRIPTION'


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2011-11-23 : 09:57:00
Damn. you're too fast. i was just about to post 'disregard', as i figgered it out meself.

thanks!!
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2011-11-23 : 10:05:46
oops. not so fast. because the 'note' field is a text type, it's complaining that 'argument data type is invalid for argument 1 of left function'.
solution?
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2011-11-23 : 11:04:26
convert the TEXT to a VARCHAR(MAX) maybe?

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-11-23 : 11:14:39
use substrint(col,1,50)

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -