SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 URGENT HELP PLEASE
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mrmmartian
Starting Member

1 Posts

Posted - 08/07/2012 :  00:41:01  Show Profile  Reply with Quote
I have this select statement:

select a.active from sys_scd_profile a
inner join sys_scd_profile_activation b on (a.id = b.profile_id)
where a.active = 1 and b.date_deactivated is not NULL and b.date_reactivated is NULL and b.date_deactivated < '7 aug 2012'

I need to update all a.active to = 0 where it exists in the select statement.

Please help. very urgent

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/07/2012 :  06:55:11  Show Profile  Reply with Quote
quote:
Originally posted by mrmmartian

I have this select statement:

select a.active from sys_scd_profile a
inner join sys_scd_profile_activation b on (a.id = b.profile_id)
where a.active = 1 and b.date_deactivated is not NULL and b.date_reactivated is NULL and b.date_deactivated < '7 aug 2012'

I need to update all a.active to = 0 where it exists in the select statement.

Please help. very urgent

You can simply add a NOT in front of the current WHERE clauses like this:
SELECT
	a.active
FROM
	sys_scd_profile a
	INNER JOIN sys_scd_profile_activation b
		ON  (a.id = b.profile_id)
WHERE
	NOT
	(
		a.active = 1
		AND b.date_deactivated IS NOT NULL
		AND b.date_reactivated IS NULL
		AND b.date_deactivated < '7 aug 2012'
	)
Alternatively, you can change each piece of the WHERE clause and use OR instead of AND as in:
WHERE
	a.active <> 1
	OR b.date_deactivated IS NULL
	OR b.date_reactivated IS NOT NULL
	OR b.date_deactivated >= '7 aug 2012'
Both should give you identical results, but I like the first one for its simplicity.

Edit: People recommend that when you want to specify dates, you specify them in the YYYYMMDD format - which is unambiguous regardless of language/culture settings. So preferable to use '20120807' instead of '7 aug 2012'

Edited by - sunitabeck on 08/07/2012 06:57:01
Go to Top of Page

lionofdezert
Aged Yak Warrior

Pakistan
864 Posts

Posted - 08/07/2012 :  06:58:06  Show Profile  Visit lionofdezert's Homepage  Send lionofdezert a Yahoo! Message  Reply with Quote
Update s_scd_profile
SET active to = 0
FROM a.active from sys_scd_profile a
inner join sys_scd_profile_activation b on (a.id = b.profile_id)
where a.active = 1 and b.date_deactivated is not NULL and b.date_reactivated is NULL and b.date_deactivated < '7 aug 2012'




--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/07/2012 :  07:17:35  Show Profile  Reply with Quote
Thanks Lion. I interpreted it as op wanted to select everything that didn't get selected by the original query!! Still too early in the morning; I am barely past the REM stage! :)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000