Solved

SQL update query syntax - SQL 2008 R2

Posted on 2014-02-20
3
362 Views
Last Modified: 2014-02-20
Hi experts,

In the data here, SAMPLE_ID_ALT needs to be updated to what I have typed in.  For every 'NS' in the SAMPLE_ID column, the SAMPLE_ID_ALT needs to be set incrementally to NS_001, NS_002 and start again when a new SITE_ID is found.  

I have tried this, and can see why the optimizer might not like it, but I cannot find a workaround.  Can anyone help please?  I would rather not use cursors if I can help it.

UPDATE GB_CB_LITH
SET SAMPLE_ID_ALT = 'NS_'+ RIGHT('000'+CAST(ROW_NUMBER()
                               OVER (PARTITION BY SITE_ID
                                     ORDER BY DEPTH_FROM)
               AS VARCHAR(3)),3)
  WHERE SAMPLE_ID = 'NS'
TEMP-LITH-DATA2.xlsx
0
Comment
Question by:colinspurs
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39873308
try this out

;WITH C AS
(
SELECT RIGHT('000'+CAST(ROW_NUMBER() OVER (PARTITION BY SITE_ID ORDER BY DEPTH_FROM) AS VARCHAR(3)),3) RN,* FROM GB_CB_LITH WHERE G.SAMPLE_ID = 'NS' 
)

UPDATE G
SET SAMPLE_ID_ALT = 'NS_'+ RN
FROM GB_CB_LITH G
JOIN C 
ON G.<PK> = C.<PK>
WHERE G.SAMPLE_ID = 'NS' 


--NOTE in the above you have to replace the PK with the primary keys

Open in new window

0
 
LVL 3

Author Comment

by:colinspurs
ID: 39873398
Superb!  Thanks.

I think you meant GB_CB_LITH G WHERE G.SAMPLE_ID = 'NS'  in line 3.
                                                  =
0
 
LVL 3

Author Closing Comment

by:colinspurs
ID: 39873400
Brilliant, thanks a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now