Possible To UPDATE Multiple Fields Conditionally?

Nov 14, 2013

I have a am doing some date calcs () . The situation is that I have a Move date (date a customer moved to a new home). I want to calculate their sales for the following 0-3 months after the move (month 0 being the move month). I have the month and year of the move (MthStart, YrStart), and I am adding 3 to MthStart to get the MthEnd of that 0-3 month period. I will then find sales BETWEEN YrStart&MthStart AND YrEnd&MthEnd (there is a YrMth field in the sales table)

Of course, for MthStarts 10, 11, and 12, the ends are 13, 14, and 15. So for these, I need to subtract 12, and increment the YrEnd by 1.

I am wondering if there is a way to update both the MthEnd and YrEnd fields at one time instead of separate SETs (or maybe I am just thinking about this the hard way to begin with). Is there a way to update both in a single CASE statement like WHEN MthEnd> 12 THEN MthEnd-12 AND YrEnd+1?

Code:
CREATE TABLE #myTable (
YrStart INT,
MthStart INT,
YrEnd INT,
MthEnd INT);

[code]....

View 4 Replies


ADVERTISEMENT

Is There A Way To Update Multiple Fields Using UPDATE Command

Oct 19, 2005

UPDATE #TempTableESR SET CTRLBudEng = (SELECT SUM(Salaries) from ProjectBudget WHERE Project = @Project)UPDATE #TempTableESR SET CTRLBudTravel = (SELECT SUM(Travels) from ProjectBudget WHERE Project = @Project)UPDATE #TempTableESR SET CTRLBudMaterials = (SELECT SUM(Materials) from ProjectBudget WHERE Project = @Project)UPDATE #TempTableESR SET CTRLBudOther = (SELECT SUM(Others) from ProjectBudget WHERE Project = @Project)UPDATE #TempTableESR SET CTRLBudContingency = (SELECT SUM(Contingency) from ProjectBudget WHERE Project = @Project)above is the UPDATE command i am using in one of my stored procedures. I have to SELECT from my ProjectBudget table 5 times to update my #TempTableESR table. is there an UPDATE command i can use which would let me update multiple fields in a table using one SELECT command?

View 1 Replies View Related

Update Multiple Fields Accross A Join

Jul 23, 2005

"David Portas" <snipped for brevity> wrote:Example 1:[color=blue]>> UPDATE table_a> SET col = ? /* Unspecified */> WHERE EXISTS> (SELECT *> FROM table_b> WHERE table_b.key_col = table_a.key_col)>[/color]<snip again>Many thanks. I have used this sample extensively since you posted it. Hopeyou (or someone) can help me with one more thing: How would it be writtento update several fields in table A with data from table B, where as youhave shown, a column matches the records?TIA!~ Duane Phillips.

View 4 Replies View Related

SQL UPDATE Conditionally

Jul 20, 2005

This is probably a stupid question, but here goes: I am trying toupdate a table differently based on what is in one of the columnswithin the table. For example, I want to set Fld1 to be "On" if Fld2is "A", and I want to set Fld1 to be "Off" if Fld2 is "B". This iseasy to do in two SQL statements, but I was wondering if there's someway to do it in a single SQL statement. Thus far, I have had no luckin figuring out how to do this. Any ideas would be appreciated!!

View 1 Replies View Related

Transact SQL :: Get One Row From Multiple Based On Fields And Also Get Sum Of Decimal Fields?

Jul 2, 2015

I am using MS SQL 2012.  I have a table that contains all the data that I need, but I need to summarize the data and also add up decimal fields while at it.  Then I need a total of those added decimal fields. My data is like this:

I have Providers, a unique ID that Providers will have multiples of, and then decimal fields. Here are my fields:

ID, provider_name, uniq_id, total_spent, total_earned

Here is sample data:

1, Harbor, A07B8, 500.00, 1200.00
2, Harbor, A07B8, 400.00, 800.00
3, Harbor, B01C8, 600.00, 700.00
4, Harbor, B01C8, 300.00, 1100,00
5, LifeLine, L01D8, 700.00, 1300.00
6, LifeLine, L01D8, 200.00, 800.00

I need the results to be just 3 lines:

Harbor, A07B8, 900.00, 2000.00
Harbor, B01C8, 900.00, 1800.00
LifeLine, L01D8, 900.00, 2100.00

But then I would need the totals for the Provider, so:

Harbor, 1800.00, 3800.00

View 3 Replies View Related

Update Fields With Searched First Date Record Fields

Jul 23, 2005

Hello !I'm trying to update one table field with another table searched firstdate record.getting some problem.If anyone have experience similar thing or have any idea about it,please guide.Sample case is given below.Thanks in adv.T.S.Negi--Sample caseDROP TABLE TEST1DROP TABLE TEST2CREATE TABLE TEST1(CUST_CD VARCHAR(10),BOOKING_DATE DATETIME,BOOKPHONE_NO VARCHAR(10))CREATE TABLE TEST2(CUST_CD VARCHAR(10),ENTRY_DATE DATETIME,FIRSTPHONE_NO VARCHAR(10))DELETE FROM TEST1INSERT INTO TEST1 VALUES('C1',GETDATE()+5,'11111111')INSERT INTO TEST1 VALUES('C1',GETDATE()+10,'22222222')INSERT INTO TEST1 VALUES('C1',GETDATE()+15,'44444444')INSERT INTO TEST1 VALUES('C1',GETDATE()+16,'33333333')DELETE FROM TEST2INSERT INTO TEST2 VALUES('C1',GETDATE(),'')INSERT INTO TEST2 VALUES('C1',GETDATE()+2,'')INSERT INTO TEST2 VALUES('C1',GETDATE()+11,'')INSERT INTO TEST2 VALUES('C1',GETDATE()+12,'')--SELECT * FROM TEST1--SELECT * FROM TEST2/*Sample dataTEST1CUST_CD BOOKING_DATE BOOKPHONE_NOC12005-04-08 21:46:47.78011111111C12005-04-13 21:46:47.78022222222C12005-04-18 21:46:47.78044444444C12005-04-19 21:46:47.78033333333TEST2CUST_CD ENTRY_DATE FIRSTPHONE_NOC12005-04-03 21:46:47.800C12005-04-05 21:46:47.800C12005-04-14 21:46:47.800C12005-04-15 21:46:47.800DESIRED RESULTCUST_CD ENTRY_DATE FIRSTPHONE_NOC12005-04-03 21:46:47.80011111111C12005-04-05 21:46:47.80011111111C12005-04-14 21:46:47.80044444444C12005-04-15 21:46:47.80044444444*/

View 3 Replies View Related

Update Fields With Data From Other Fields In Same Row

Jun 30, 2000

Pardon me if this question is too elementary. I am trying to create a trigger that will cause certain datafields to be updated with values from other data fields in the same row when a certain column, created specifically to fire the trigger, is updated. The purpose of this is to reduce data entry by field personnel.I think I have the create trigger statement correct, but I'm a little confused on the update statement.

In a nutshell, how can I write something like:
UPDATE "TABLENAME"
SET DATAFIELD1 = DATAFIELD2
WHERE RECORDNUMBER = (THE SAME RECORD NUMBER)

I do know that I have to ensure that sp_dboption Recursive Triggers value is set to false, thanks.

View 2 Replies View Related

SQL Server 2008 :: How To Update Multiple Column With Multiple Condition

Feb 25, 2015

I need to update multiple columns in a table with multiple condition.

For example, this is my Query

update Table1
set weight= d.weight,
stateweight=d.stateweight,
overallweight=d.overallweight
from
(select * from table2)d
where table1.state=d.state and
table1.month=d.month and
table1.year=d.year

If table matches all the three column (State,month,year), it should update only weight column and if it matches(state ,year) it should update only the stateweight column and if it matches(year) it should update only the overallweight column

I can't write an update query for each condition separately because its a huge select

View 7 Replies View Related

Search Multiple Fields Using Like

Mar 2, 2007

I've done some simple sql's for searching a field using Like,But this one is different.
I am adding a param named @searchText
I would like to bring back all records in all the fields listedbelow that has that string in the field...
WHERE a.manufacturer = b.manufacturerIDAND a.location = c.locationIDAND a.Status = d.statusIDAND a.EquipmentType = e.IDAND a.calLab = f.ID AND a.testTechnology = g.id AND (c.locationID = @location OR @location = 0)
So, each line/field above I want to search for the string and includein the dataset.
Anyone can point me in the right direction?
Thanks,
Zath

View 7 Replies View Related

Dropdownlist With Multiple Fields

Sep 24, 2007

How can I get two fields to appear side by side in the DropDownList?  Ex.  FName and LName
 
Thanks

View 1 Replies View Related

Merging Multiple Fields

Mar 20, 2001

Please point me in the direction of a tutorial that will do help me do the following:

The database has been previously created and the users first & last names are in the respective columns. There is also a column that should contain the full name of the user but as such it only contains <NULL>. Is there a way to pull the first name and last name from the db then write both names to column3?

Thanks - Sharon

View 5 Replies View Related

Checking Multiple Fields At Once

Sep 29, 2005

I have a table that tracks some dates, and I am looking for an SQL statemet that will check all of the fields to see if a prespecified data range is true. For exampe you enter a start and end date and then then the SQL statement would check about 12 different fields to see if any of the dates are within that range. I am trying to use an or statement, but to no avail. So if you have any help it would be greatly appreciated.

View 1 Replies View Related

GROUP BY With Multiple Fields

Jun 12, 2014

I have two issues I'm trying to deal with in my code.

1. I'm trying to group by first and last name, which are in two different columns
2. I'm trying to take an average of pay per miles.

Neither of these is working well. Actually, neither is working at all.

This is the code!

SELECT
OD.DriverID,
(W.FirstName + W.LastName AS 'Driver'),
DATEADD(dd,(DATEDIFF(dd,0,O.ReadyTimeFrom)),0) AS Date,
DATENAME(dw,O.ReadyTimeFrom) AS DayOfTheWeek,
Count(OD.OrderID) AS 'Total Orders',
SUM(O.Distance) AS OrderMiles,

[Code] ....

View 2 Replies View Related

Getting Count With Multiple Fields

Sep 6, 2005

Hi all,I'm running into a road block, and I know I've done this before. I'mgetting fields from two tables, and I need to do a count of similaritems with it showing some extra info.Here's my fields:Log.LogId - IntLog.LogDispatcherID - IntOfficer.OfficerID - IntOfficer.OfficerFirstName - VarcharOfficer.OfficerLastName - VarcharI can get the info I need without a count with this:select a.LogID,a.LogDispatcherID,b.OfficerFirstname + ' ' + b.OfficerLastname as OfficerNamefrom[Log] a, Officer bwhere a.LogAssigned1 = b.OfficerIDBut when I try to add a count and group-by it errors out:select Count(a.LogID) as LogCount,a.LogDispatcherID,b.OfficerFirstname + ' ' + b.OfficerLastname as OfficerNamefrom[Log] a, Officer bwhere a.LogAssigned1 = b.OfficerIDGroup By a.LogIDI've done this before, but this isn't working. It's giving the error"it is not contained in either an aggregate function or the GROUP BYclause" for each field other then LogID.How can I do this? I want output similar to this:LogCountLogDispatchIDOfficerName334Tom Jones422John Smith.... EtcThanks for any suggestions or ideas...Sam Alex

View 2 Replies View Related

Updating Multiple Fields

Sep 22, 2005

How can I update each record in a table, based on a value in another tablewith a single SQL statement?For example, suppose I have the following two tables:Table1Name Something Color-----------------------------------------John GHAS BlueJohn DDSS BlueJohn EESS BluePaul xxxx RedRingo HJKS RedRingo FFFS RedSara hjkd PurpleSara TTHE PurpleJimi sdkjls GreenTable2Name Color------------------------John ?Paul ?Ringo ?Sara ?Jimi ?How can I update the color field in table 2 to correspond with the colorfield in table1 (so I can normalize the db and delete the color field fromtable1)?I know I could open table2 and loop through within my app; just wonderingabout a single SQL statement that would do it. I need a similar technique inother places as part of my app.Thanks,Calan

View 5 Replies View Related

Help ! Multiple Fields Using IN Condition....

Oct 27, 2006

Hi All,

I am trying to do something like this

Select * from Table 1 where (field1, field2, field3) IN (select field1, field2, field3 from table2 where fieldx = 'rt')

This can be done in oracle. Is there any equivalent in SQL Server ?

Can anybody please help !

Thanks.

PN

View 3 Replies View Related

Help ! Using Multiple Fields With IN Condition !

Oct 27, 2006



Hi All,

I am trying to do something like this

Select * from Table 1 where (field1, field2, field3) IN (select field1, field2, field3 from table2 where fieldx = 'rt')

This can be done in oracle. Is there any equivalent in SQL Server ?

Can anybody please help !

Thanks.

PN

View 3 Replies View Related

Plz Help...update Value In Multiple Db Table Using Single 'update Command'

Mar 18, 2005

hi,friends

we show record from multiple table using single 'selectcommand'.
like....
---------
select *
from cust_detail,vend_detail
---------

i want to insert value in multiple database table(more than one) using single 'insert command'.
is it possible?
give any idea or solution.

i want to update value in multiple database table(more than one) using single 'update command'

i want to delete value in multiple database table(more than one) using singl 'delete command'
it is possible?
give any idea or solution.

it's urgent.

thanks in advance.

View 2 Replies View Related

Update 10000 Fields

Nov 15, 2007

I have a list of 10000 websites that need to be updated with certain data.

So my question is would i be able to get SQL to lookup a certain list of websites? And then me update the required fields?

Its prob an easy answer, thanks!

View 9 Replies View Related

How To Enable The SQL To Update All Fields?

Nov 9, 2006

How to enable the SQL to update all fields?

"UPDATE addresses SET "

+ "strCompany='" + strCompany + "',"

+ "Name='" + strName + "',"

+ "strAddress='" + strAddress + "',"

+ " strPhone='" + fields.phone.getText() +
"',"

+ " strHp='" + fields.hp.getText() + "',"

+ " strFax='" + fields.fax.getText() +
"',"

+ " strEmail='" + strEmail + "',"

+ " strStart='" +
fields.start.getText().trim() +
"',"

+ " intDay= '" + Integer.valueOf(dd) +
"',"

+ " intMonth='" + Integer.valueOf(mm) +
"',"

+ " intYear='" + Integer.valueOf(yy) +
"',"

+ " strMrc='" + fields.mrc.getText() +
"',"

+ " strIsp='" + fields.isp.getText() +
"',"

+ " strDes='" + fields.des.getText() +
"',"

+ " strSale='" + fields.sale.getText() +
"',"

+ " strContract='" +
fields.contract.getText() +
"',"

+ " strMark='" + fields.mark.getText() +
"'"

+ " WHERE strCompany='" + strCompany

+ "'"
+ "AND strName='" + strName + "'";

View 1 Replies View Related

Inserting To Multiple Fields Using A Button

Jul 22, 2006

I have multiple textboxes in a page. How do i make them insert their values to multiple fields on multiple tables using a button.

View 1 Replies View Related

Aggregate With Multiple Fields Question

May 7, 2008

I'm having trouble using Aggregate functions with multiple fields.
Here is what I'm trying to do:

SELECT REPLACE(Cust_Key, sum(PRODUCT_CHARGE), count(Tracking_Number), Tracking_Number FROM Shipments = '2008-05-05'
Group By Cust_Key, Tracking_Number

I can get this to work when I use Group By for a single field like Cust_Key but I don't know how to make it work for multiple fields. What is the best way to write something that contains a few aggregate functions on top of multiple fields?

Thanks

View 4 Replies View Related

Using Multiple Fields As The Unique Identifier

Jun 10, 2008

Please see below post

View 2 Replies View Related

Using An Expression To Group On Multiple Fields

Mar 23, 2008



I have an exisitng report that lists unit name, provider, runday, shift, patient. The report groups by unit name and there is a page break after each unit name. So in the current environment the report prints one page per unit that contains all of the providers, rundays, shifts, and patients for that unit name its grouping on. So the report would like like this when it prints:

Unit A
Provider 1 Runday 1 Shift 1





patient 1
patient 2
Provider 1 Runday 1 Shift 2





patient 1
patient 2
Provider 1 Runday 2 Shift 1





patient 1
patient 2
Provider 1 Runday 2 Shift 2





patient 1
patient 2
Provider 2 Runday 1 Shift 1





patient 1
patient 2
Provider 2 Runday 1 Shift 2





patient 1
patient 2
Provider 2 Runday 2 Shift 1





patient 1
patient 2
Provider 2 Runday 2 Shift 2





patient 1
patient 2
PAGE BREAK
Unit B............(repeat data from page 1)
PAGE BREAK
Unit C............(repeat data from page 1

The end user would like the ability to keep the report as is but would like to also be able to print the report as one page per each unit name, provider, runday and shift. so it would look like this

Unit A
Provider 1 Runday 1 Shift 1





patient 1
patient 2
PAGE BREAK
Unit A
Provider 1 Runday 1 Shift 2





patient 1
patient 2
PAGE BREAK
Unit A
Provider 1 Runday 2 Shift 1





patient 1
patient 2




So I created a boolean parameter with a prompt of Page break by Unit, Provider, Runday & Shift? My thought is if the users sets this to False, the report will group on just Unit Name (the first example). If the user sets this to True, the report will group on Unit Name, Provider, Runday & Shift.

I set the grouping expression for this data table as:
=iif(Parameters!Grouping.Value = "False", Fields!unit_Name.Value,(Fields!unit_Name.Value,Fields!Lname.Value,Fields!Rundays.ValueFields!Shift.Value))


Within the expression editor window it displays a syntax error and the report will not run.

Any help would be greatly appreciated!!!!!!!!!

View 7 Replies View Related

Multiple Updates And Identity Fields

Sep 27, 2006

I have a table used by multiple applications. One column is an Identify field and is also used as a Primary key. What isare the best practices to use get the identity value returned after an INSERT made by my code.. I'm worried that if someone does an INSERT into the same table a "zillionth" of a second later than I did, that I could get their Identity value.



TIA,



Barkingdog

View 4 Replies View Related

Transact SQL :: Concatenation Of Multiple Fields

Oct 29, 2015

I have the folloiwng fields:

ProbationLengthYears AS Decimal
ProbationLengthMonths AS Decimal
ProbationLengthDays AS Decimal

Sample Data:

ProbationLengthYears = 2.00
ProbationLengthMonths 0.00
ProbationLengthDays 0.00

I have successfully concatenated and converted all three fields into one field as Follow:

 SELECT
  CONVERT(varchar(10),CAST(cc2.ProbationLengthYears AS INT)) + ' Year(s)' + ' ' +
  CONVERT(varchar(10),CAST(cc2.ProbationLengthMonths AS INT)) + ' Month(s)' + ' ' +
  CONVERT(varchar(10),CAST(cc2.ProbationLengthDays  AS INT)) + ' Day(s)'

 The result is:

  2 Year(s) 0 Month(s) 0 Day(s)

Ideally I would like the result to be as follow because the probation months and days are 0:

  2 Year(s).

View 4 Replies View Related

Transact SQL :: Concatenate Multiple Fields Into One

Dec 5, 2015

I have a table where I need to concatenate all values into one field separated by a comma.  If the field is null display a blank value.  This is my table structure and example output

Create Table #read
(
id int
,field1 varchar(100)
,field2 varchar(100)
,field3 varchar(100)
,field4 varchar(100)

[code]...

View 5 Replies View Related

Concatenating Fields Over Multiple Rows

Jul 13, 2007

I have a query that produces seperate rows for people, but I want to combine them into one place. I tried doing this in SQL but apparently it's not very easy in SQL Server. You need to loop through a table using cursors. I'm not quite that advanced with SQL Server and was wondering if there might be an easier way just using SSRS.

In other words I have a table as such:

1 John Smith
2 Jane Doe
3 Matthew Jones

And I'd like to create one textbox that contains the following:

"John Smith, Jane Doe, Matthew Jones"

I've been drawing a blank. Anyone have any ideas?

Levi

View 4 Replies View Related

Update Certain Fields From A Grid View ?

Sep 25, 2006

Hi all,I asked a similar question a few weeks ago but I didn't explain myself to well so I was hoping for some more input.I have created a webpage in Visual web developer that contains a gridview that points to an SQL table.All works well as far as searching the table and sorting.  The SQL table itself is Droped ( deleted ) and then re-created every 1 minute from a query to a linked DB2 database  so that we have a "live" table of our DB2 system but in SQL.....However,There is now a requirement to be able to ADD data to this SQL table. eg: a comments field.. from the website.My procedure for re-creating the SQL DB is now useless as any data added through the website would be lost when the table gets re-created.What is the best way for me to achieve a permanent table in SQL that gets updated from the Linked server DB2 but that I can also add comments from the front end (Website Gridview)..I'm assuming some sort of update query and a join to a seperate, permanent table that contains the comments?.Please help. Ray.. 

View 1 Replies View Related

Update Query That Only Updates Certain Fields

May 28, 2006

I'm not a T-SQL expert so I'm calling on the pros. I want to design a stored procedure that updates only fields  in a given record for which a non-null parameter is specified.
Example:
Create dbo.MySproc
   @CustomerID int,
   @CustomerFirstName varchar(50)=NULL,
   @CustomerLastName(50)=NULL
AS
UPDATE Customers
SET
   CustomerFirstName= @CustomerFirstName , //update only if non-null
   CustomerLastName= @CustomerLastName   //update only if non-null
WHERE CustomerID = @CustomerID
RETURN
So say when calling sproc if @CustomerFirstName is specified but @CustomerLastName is not, the field CustomerFirstName would be updated but field CustomerLastName would retain original data.
Thanx in advance!
 

View 2 Replies View Related

Search And Update All Fields That Contains String

Jan 8, 2008

I hava at large database and I need to search all tables for the string 'NaN' and replace it to null to enable conversion.

Is there any way to do this so that i do not need to write an update query for every column?

Would appretiate an example if anyone knows how to manage this.

Thanks!

View 2 Replies View Related

Many Fields Update From A Group By Clause

Jul 20, 2005

Hi All,In Oracle, I can easily make this query :UPDATE t1 SET (f1,f2)=(SELECT AVG(f3),SUM(f4)FROM t2WHERE t2.f5=t1.f6)WHERE f5='Something'I cannot seem to be able to do the same thing with MS-SQL. There areonly 2 ways I've figured out, and I fear performance cost in both cases,which are these :1)UPDATE t1 SET f1=(SELECT AVG(f3)FROM t2WHERE t2.f5=t1.f6)WHERE f5='Something'and then the same statement but with f2, and2)UPDATE t1 SET f1=(SELECT AVG(f3)FROM t2WHERE t2.f5=t1.f6),f2=(SELECT SUM(f4)FROM t2WHERE t2.f5=t1.f6)WHERE f5='Something'Is there a way with MS-SQL to do the Oracle equivalent in this case ?Thanks,Michel

View 3 Replies View Related

Update Field Based Upon Other Fields

Jan 24, 2008



Hi,

I have a table with eight (8) fields, including the primary key (rfpid). Three of the fields are foreign keys, which take their values form lookup tables. They are int fields (pmid, sectorid, officeid). One of the fields in this table is based on putting together the descriptive field in the lookup table for sector (tblsector). The two other fields to be part of this string are the rfpname and rfpid. This creates the following string:

rfpsector_nameproposalscurrent_year
fpname_08_rfpid


NOTE:


The words rfp, proposals are words that have to be part of string;

the slashes are to also appear.

current_year would be defaulting to datepart = year (2008)

The part that has the last two digits of the current year then the underscore and then the rfpid should be connected by an underscore to the rfpname.
I am at a loss and would greatly appreciate any help.

Thanks

View 51 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved