Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 19th, 2007, 09:14 PM
Authorized User
 
Join Date: Jul 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default Udate Query

Hello All

I want to write a query that will update a field "A" based on weather another field "B" contains something in field "C"

Eg:

Valuation_no "B" rate_code_rate "C" origen_rate_code "A"
03710/003.00 2000 02000
03710/003.00 3020 03020

Basically i want to say that if "valuation_no" contains "rate_cade_rate" of 2000 and 3020 then make "origen_rate_code" =02000 where "rate_code_rate" = 2000 and the same for where "rate_code_rate" = 3020 make "origen_rate_code" = 03020.

Thanks

 
Old April 20th, 2007, 08:51 AM
Authorized User
 
Join Date: Oct 2005
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try it this way:

UPDATE YourTable
SET origen_rate_code = RIGHT('00000' + rate_code_rate, 5)
WHERE valuation_no LIKE '%' + rate_code_rate + '%'

SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp
 
Old April 22nd, 2007, 03:46 PM
Authorized User
 
Join Date: Jul 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Thanks for the reply but it did not do as i want (I probably did not explain properly). What I need to do is where a valuation no has a rate_code_rate of 2000 and 3020, then make origen_ _code = 02000 (for rate_code_rate of 2000 and = 03020 (for rate_code_rate of 3020)

Thanks

 
Old April 23rd, 2007, 01:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Then try this.
Code:
UPDATE <YourTable>
SET origen_rate_code = RIGHT('00000' + rate_code_rate, 5)
WHERE rate_code_rate in (2000, 3020)
Cheer

_________________________
- Vijay G
Strive for Perfection
 
Old April 23rd, 2007, 03:39 PM
Authorized User
 
Join Date: Jul 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Vijay

How would I write this query if there were valuation no's that had a rate_code_rate of 1000, 2000 and 3020 but i only wanted to make origen_rate_code = 02000 and 03020 where valuation no only had 2000 and 3020 and not 1000, 2000 and 3020?

Thanks for your reply it did work

Greg

 
Old April 26th, 2007, 09:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sorry for the delayed response.

The update statement I posted above does that. It checks only for 2000 and 3020 and updates only those. So I believe it is taken care of.

cheers.

_________________________
- Vijay G
Strive for Perfection
 
Old April 26th, 2007, 09:56 PM
Authorized User
 
Join Date: Jul 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Vijay

Thanks for the reponce. Your script does work but this is my problem. some valuation no has only got rate_codes of 2000 and 3020 and some have 1000, 2000 and 3020. I only want to change those where a valuation no has 2000 and 3020. here is a copy of my extract

valuation_no rate_code origen_rate_code
03710/001.00 123 1230
03710/001.00 3020 123
03710/001.00 2000 123
03710/001.00 1000 123
03710/002.03 2000 123
03710/002.03 3020 123
03710/002.03 401 4010
03710/002.03 1000 123
03710/003.00 2000 123
03710/003.00 3020 123

Where the origen_rate_code = 123 for valuation no containing 1000,2000,3000 I want to keep but only change to 02000 and 03020 where valuation no only has 2000,3020and not 1000

Thanks

Greg


 
Old April 27th, 2007, 12:24 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Use CASE operator.


 
Old April 27th, 2007, 07:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Try this...
Code:
UPDATE test
SET origen_rate_code = RIGHT('00000' + rate_code_rate, 5)
 WHERE valuation_no NOT IN 
( SELECT valuation_no FROM test WHERE rate_code_rate IN (1000) )
AND rate_code_rate in (2000, 3020)
Just added the one marked in BLUE

Cheers
_________________________
- Vijay G
Strive for Perfection
 
Old May 2nd, 2007, 03:58 PM
Authorized User
 
Join Date: Jul 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Vijay this worked. Muck appreciated
Greg






Similar Threads
Thread Thread Starter Forum Replies Last Post
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
how to make a query from an existing query raport SQL Language 3 November 13th, 2006 08:59 PM
I solved insert query.now see this Update Query. [email protected] VB.NET 2002/2003 Basics 2 September 21st, 2006 12:48 AM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM
Error on Make-Table Query In Union Query rylemer Access 1 August 20th, 2003 07:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.