Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Value of column dependent on the values of others. How can I do it . Please help ! Expand / Collapse
Author
Message
Posted Friday, December 14, 2012 6:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, December 17, 2012 6:49 AM
Points: 5, Visits: 25
Hello , this is the first time i'm posting here, but this time i really need your help.
I need to write a query to update a table so the value of a specific column is dependent of the values of all other columns in the row.

EX
table 1

Val1 | Val2 | Val3 | Val4|...
e | b | . | u |


I need the Val4 value to be 'u' is there's no column with '.' and 'U' is there is. The table i'm talking about has a lot of columns. Please help ASAP

Thank you
Post #1396624
Posted Friday, December 14, 2012 6:59 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, January 28, 2013 1:45 AM
Points: 386, Visits: 199
update tbl
set u = case when val1 = '.' or val2 = '.' or val3 = '.' then 'U' else 'u' end



Cursors never.
DTS - only when needed and never to control.
Post #1396631
Posted Friday, December 14, 2012 7:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, December 17, 2012 6:49 AM
Points: 5, Visits: 25
Thank you for this but the problem is that I'm working with a table that has over 80 columns and I was thinking if there's an different, easyer way to do it.
Post #1396635
Posted Friday, December 14, 2012 7:20 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:45 PM
Points: 8,567, Visits: 8,218
dragosgrigs (12/14/2012)
Thank you for this but the problem is that I'm working with a table that has over 80 columns and I was thinking if there's an different, easyer way to do it.


Nope. You are going to have to look at each column.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1396646
Posted Friday, December 14, 2012 7:21 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, May 16, 2013 9:16 AM
Points: 2,236, Visits: 6,486
If you execute this, it's at your own risk. Do it in a test environment first and make sure you understand what is going on.

DECLARE @Table_Name VARCHAR(20) = 'table 1', @Update_Column VARCHAR(20) = 'Val4', @SQL NVARCHAR(MAX);

SELECT @SQL = 'UPDATE '+QUOTENAME(@Table_Name)+CHAR(13)+CHAR(10)+'SET '+QUOTENAME(@Update_Column)+' = CASE WHEN ' +
STUFF((SELECT ' OR ' +val
FROM (SELECT name+'='+CHAR(39)+'.'+CHAR(39)
FROM sys.columns
WHERE object_id = OBJECT_ID(@Table_Name)
AND name <> @Update_Column
)a(val)
FOR XML PATH(''), TYPE
).value('.','NVARCHAR(MAX)'),1,4,'')+' THEN '+CHAR(39)+'U'+CHAR(39)+' ELSE '+CHAR(39)+'u'+CHAR(39)+' END';

EXECUTE sp_executesql @SQL;




Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1396648
Posted Friday, December 14, 2012 7:25 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:45 PM
Points: 8,567, Visits: 8,218
You could do it a little easier with some string manipulation.

;with cte (val1, val2, val3)
as
(
select 'asdf', 'qwer', 'rtyu' union all
select 'as.df', 'yt', 'hhg' union all
select 'weret', '99t4.rtkg9', ''
)

select case when CHARINDEX('.', val1 + val2 + val3) > 0 then 'U' else 'u' end
from cte

If your columns allow NULL you would need to wrap each column with an ISNULL(val1, '').

That is at least a little less typing.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1396652
Posted Friday, December 14, 2012 7:37 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 6:32 AM
Points: 1,606, Visits: 1,088
If the table has a key column another option would be to UNPIVOT it into a temp table and then do your update based on that. For example you could create a table called #AllColumns which looks like:
KeyVal ColVal
123 asdf
123 qwe
123 rt.y
so there is one row for each row/column combination in your current table.

Then you just do
UPDATE FirstTable SET LastCol =
CASE WHEN EXISTS (
SELECT 1 FROM #AllColumns
WHERE KeyVal = FirstTable.KeyVal
AND CHARINDEX('.',ColVal) > 0)
THEN 'U' ELSE 'u' END;

Hope that makes some sort of sense!
Post #1396660
Posted Friday, December 14, 2012 7:51 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, December 17, 2012 6:49 AM
Points: 5, Visits: 25
Thank you ! This worked. Much appreciated it
Post #1396670
Posted Friday, December 14, 2012 7:52 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, December 17, 2012 6:49 AM
Points: 5, Visits: 25
Thank you all for the prompt responses. Damn, you guys are good
Post #1396671
Posted Friday, December 14, 2012 7:20 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 32,903, Visits: 26,784
dragosgrigs (12/14/2012)
Thank you all for the prompt responses. Damn, you guys are good


I have a question, though. What do all of these columns contain? Do they contain just 1 character each, for example?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1396849
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse