SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update Part of a SQL field with charindex


Update Part of a SQL field with charindex

Author
Message
duncanlarry
duncanlarry
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 13
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
Jason Selburg
Jason Selburg
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3717 Visits: 4110
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 you'll likely increase the number and quality of responses you get to your question.

Jason L. Selburg
duncanlarry
duncanlarry
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 13
Thank You!!!

That STUFF function did exactly what I needed it to do.

Thanks Again.

:-D
Sachin Nandanwar
Sachin Nandanwar
Say Hey Kid
Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)

Group: General Forum Members
Points: 693 Visits: 2633
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
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16526 Visits: 19557
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



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
Sachin Nandanwar
Sachin Nandanwar
Say Hey Kid
Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)Say Hey Kid (693 reputation)

Group: General Forum Members
Points: 693 Visits: 2633

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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search