June 23, 2014 at 12:45 pm
Hello,
I have a field and the data is in the format below:
12-FLA-2-02
I am trying to update just the first part of the data before the first dash which is 12 to another field that is a single numeric value.
update table set table.field = LEFT(table.field, CHARINDEX('-', table.field)-1) where table.field is null
I am trying to manipulate this script below, but I cannot figure out how to update just the 12 before the dash.
Thanks
Larry
June 23, 2014 at 1:15 pm
Read up on STUFF .. http://msdn.microsoft.com/en-us/library/ms188043.aspx
______________________________________________________________________
Personal Motto: Why push the envelope when you can just open it?
If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.
Jason L. SelburgJune 23, 2014 at 2:39 pm
Thank You!!!
That STUFF function did exactly what I needed it to do.
Thanks Again.
June 24, 2014 at 1:52 am
You can use REPLACE function as well.
declare @str as table( val varchar(20))
insert @str
select '12-FLA-2-02'
declare @update varchar(20)='11111'
select * from @str
update @str set val=replace(val,LEFT(val, CHARINDEX('-', val)-1) ,@update)
select * from @str
--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
June 24, 2014 at 2:04 am
Sachin Nandanwar (6/24/2014)
You can use REPLACE function as well.
declare @str as table( val varchar(20))
insert @str
select '12-FLA-2-02'
declare @update varchar(20)='11111'
select * from @str
update @str set val=replace(val,LEFT(val, CHARINDEX('-', val)-1) ,@update)
select * from @str
You can, but I wouldn't recommend it:
declare @str as table( val varchar(20))
insert @str select '12-FLA-2-02'
insert @str select '12-FLA-2-12'
declare @update varchar(20)='11111'
select * from @str
update @str set val=replace(val,LEFT(val, CHARINDEX('-', val)-1) ,@update)
select * from @str
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 24, 2014 at 2:42 am
You can, but I wouldn't recommend it:
declare @str as table( val varchar(20))
insert @str select '12-FLA-2-02'
insert @str select '12-FLA-2-12'
declare @update varchar(20)='11111'
select * from @str
update @str set val=replace(val,LEFT(val, CHARINDEX('-', val)-1) ,@update)
select * from @str
Yes makes sense.Thanks for pointing that out..
--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy