Update table all null values to 0 single query
hi dear ;
How Can I do , update table all null values to 0 using single query or procedure
declare @tableName nvarchar(100)
declare @querys varchar(max)
set @querys = ''
set @tableName = 'YOUR TABLE NAME HERE'
select @querys = @querys + 'update ' + @tableName + ' set ' +
QUOTENAME(t.[name]) + '=0 where ' + QUOTENAME(t.[name]) + ' is null;'
from (SELECT [name] FROM syscolumns
WHERE id = (SELECT id
FROM sysobjects
WHERE type = 'U'
AND [NAME] = @tableName))t
select @querys
execute sp_executesql @sqlQuery
Reference:
http://stackoverflow.com/questions/6130133/sql-server-update-all-null-field-as-0
-Vaibhav Chaudhari
this code is return update TABLE set [FIELD]=isnull([FIELD],''),update TABLE set [FIELD2]=isnull([FIELD2],'')
I want to use UPDATE TABLE SET FIELD1=ISNULL(FIELD1,0),FIELD2=ISNULL(FIELD2,0),FIELD3=ISNULL(FIELD3,0) So CUT another update and set statement .
Similar Messages
-
how to transfer database table contain null values, primary key, and foreign key to the another database in same server. using INSERT method. thanks
INSERT targetdb.dbo.tbl (col1, col2, col3, ...)
SELECT col1, col2, col3, ...
FROM sourcedb.dbo.tbl
Or what is your question really about? Since you talke about foreign keys etc, I suspect that you want to transfer the entire table definition, but you cannot do that with an INSERT statement.
Erland Sommarskog, SQL Server MVP, [email protected] -
Count the all null values in the given table
Suppose a table has n number of rows and columns .
can any one tell the query to count the number of null values present in the table.
For example :
select * from tname:
A B
cc jhghf
hff -
- kjh
tys -
- gyrfg
jjgg -
jfgh -
- uytg
10 rows selected..
now to count the null vales in the table we can use ---> select count(1)-count(a)+count(2)-count(b) as ncount from tname;
o/p:
ncount
11
now my questions is we have n number of columns now pls tel tell me how to count the nulls present in table..?
Thanks in Advance.Object[][] o;
int tot = 0;
for(int x = 0 ; x < o.length ; x++)
tot += o[x].length;
}If it's not a jagged array, however, this would be easier:
int tot = o.length * o[0].length; -
Updating TABLEA based on values in TABLEB
Hi: I have the two following tables. I want to write a query which updates fields in TABLE1 based on certain values in TABLE2
1. Update SEVERITY in TABLE1 with SVRT_CD in TABLE2 for all TABLE1.INCIDENT = TABLE2.TKT_NBR
2. Update ESCALATION in TABLE1 with ESCL_LVL in TABLE2 for all TABLE1.INCIDENT = TABLE2.TKT_NBR
3. If the TABLE2.MNS_STATUS is 5 then set TABLE1.INCIDENTSTATUS='Acls' for all TABLE1.INCIDENT = TABLE2.TKT_NBR
4. If the TABLE2.MNS_STATUS is 4 then set TABLE1.INCIDENTSTATUS='Bctk' for all TABLE1.INCIDENT = TABLE2.TKT_NBR
remedy@MDS> descr TABLE1;
USERID NOT NULL VARCHAR2(32)
INCIDENT NOT NULL VARCHAR2(64)
INCIDENTSTATUS NULL VARCHAR2(16)
SEVERITY NULL NUMBER
ESCALATION NULL NUMBER
ONSTRAINT user_pk PRIMARY KEY (USERID,INCIDENT)
netcool@RMDSP02> describe TABLE2;
TKT_NBR NULL NUMBER(10,0)
ESCL_LVL NULL NUMBER(2,0)
SVRT_CD NULL VARCHAR2(1)
MNS_STATUS NULL NUMBER(1,0)
CONSTRAINT tkt_pk PRIMARY KEY (TKT_NBR)I think this will do what I want in #1 and #2. Can someone confirm?
UPDATE TABLE1
SET (SEVERITY, ESCALATION) = (SELECT SVRT_CD,ESCL_LVL from TABLE2 where to_number(TABLE1.INCIDENT) = TABLE2.TKT_NBR)Not sure how do I do the #3 and #4.
Thanks
RaviFor point 3 and 4 use decode. and dont forget to use EXISTS in your update.
UPDATE table1
SET (severity,
escalation,
incidentstatus) = (SELECT svrt_cd,
escl_lvl,
DECODE(mns_status,5,'Acls',4,'Bctk')
FROM table2
WHERE TO_NUMBER(table1.incident) = TABLE2.TKT_NBR)
WHERE EXISTS (SELECT NULL
FROM table2
WHERE TO_NUMBER(table1.incident) = TABLE2.TKT_NBR) Thanks,
Karthick. -
Is it possible to filter a table for NULL values in a column? (11g)
I am using the out-of-the box table filtering provided by ADF Faces RC, but cannot figure out how to filter on Null value that are in the table.
I have tried "IS NULL" and "= NULL", but nothing works.
I have a backing bean that intercepts the filtered values before the filter is applied, and I can't set it there either.
Does anyone know how this is accomplished?No, it is not. Worse, many people will view this in any old PDF viewer, most of which won't have overprint preview at all.
You need to send very clear instructions about this, with specific screen shots - or better, avoid tools and constructions that require it. Probably some wording about "need to view with suitable professional tools" will make the message more palatable. -
Update table, exclude NULL rows...
Hi all,
I have 2 tables.
TABLE_A
SRNO VARCHAR2(10)
FLAG VARCHAR2(20)
TABLE_B
SNO VARCHAR2(10)
IND VARCHAR2(20)
I wan to update the FLAG in TABLE_A with IND from TABLE_B where SRNO from both table matches.
here i have written
UPDATE TABLE_A A
SET A.FLAG = (SELECT B.IND FROM TABLE_B B WHERE B.SNO = A.SRNO)
Here, problem is, i do not want to update SRNO in TABLE_A where IND is null in TABLE_B. (or exclude the SNO from innerquery where IND is null)
How can i modify this query to do it?
I have tried using
UPDATE TABLE_A A
SET A.FLAG = (SELECT B.IND FROM TABLE_B B WHERE B.SNO = A.SRNO AND B.IND IS NOT NULL)
when i execute this query, still it updates all rows and updates FLAG to null where IND is null in table B for same SRNO.Hi,
Try
UPDATE TABLE_A A
SET A.FLAG = (SELECT B.IND FROM TABLE_B B WHERE B.SNO = A.SRNO AND B.IND IS NOT NULL)
WHERE EXISTS (SELECT B.IND FROM TABLE_B B WHERE B.SNO = A.SRNO AND B.IND IS NOT NULL)*009* -
Joining 3 tables with null values
Hi,
I have three tables that have varying data in. I want to write a query which will list all three in the result set, but when one of the tables doesn't have a row, write in No Value.
I will be intending on joining the tables with account numbers. What will be the syntax of a join with three tables where I am not sure which table will have null values?
Thanks...Something like
select nvl(a.id,'No Value'), nvl(b.id, 'No Value'), nvl(C.id, 'No Value')
from (select id from t1) a,
(select id from t2) b,
(select id from t3) c
where a.id = b.id(+)
and a.id = c.id(+)
Hi,
I have three tables that have varying data in. I want to write a query which will list all three in the result set, but when one of the tables doesn't have a row, write in No Value.
I will be intending on joining the tables with account numbers. What will be the syntax of a join with three tables where I am not sure which table will have null values?
Thanks... -
Get a Column value based on other column value in a single query
Hi All,
I have a problem here -
In Table XYZ I have columns by name A, B, C and COL_I. COL_I has a value A or B or C. Based on the value in COL_I, I need to get the value from the corresponding column in the table.
For Ex: If the COL_I has the value as 'A' then I need to fetch the value from the column A. If it is 'B' then fetch from column B.
This has to be done in a single query.
Thanks,
san_mahHi You can use this query
I have taken this simple case
SQL> desc column_fetch
Name Null? Type
C_FIRST_NAME VARCHAR2(30)
C_MIDDLE_NAME VARCHAR2(30)
C_LAST_NAME VARCHAR2(30)
C_GET_NAME VARCHAR2(30)
based on C_GET_NAME find values in columns C_FIRST_NAME,C_MIDDLE_NAME,C_LAST_NAME
Values in Table
SQL> select * from column_fetch
2 ;
C_FIRST_NAME C_MIDDLE_NAME C_LAST_NAME C_GET_NAME
A B C D
A B C F
A B C F
A B C A
A B C B
A B C C
CASE Statement:
SELECT
CASE WHEN c_first_name=c_get_name THEN c_first_name
WHEN C_MIDDLE_NAME=C_GET_NAME THEN C_MIDDLE_NAME
WHEN C_LAST_NAME=C_GET_NAME THEN C_LAST_NAME
ELSE 'Nothing' END
FROM column_fetch; -
Show NULL values in an SQL query
Hi,
Please excuse my ignorance, but I am very green in Oracle.
I am trying to create a simple function in PHP to return a list of the entire table row in an Oracle DB, however no matter I do Oracle skipps the columns with NULL values, which means that I don't even get the number of the columns corect. The query is as simple as it can get:
SELECT * FROM table_name;
I've tried NVL, DECODE and god knows what else. Please give me a hand with this.
Cheers,
PimmyOnce you can use ODBC it should be possible to use MySQL with HS:
http://www.idevelopment.info/data/Oracle/DBA_tips/Heterogeneous_Services/HETERO_2.shtml
What concerns the licenses: We are not referring to transparent gateways but to heterogenous services (generic connectivity) which come free with the DB:
http://www.oracle.com/technology/products/gateways/faq.html#A707 -
JOIN over 3 tables with 'null' values?
Hello all,
I would like to join data from 3 tables, whereas from the 3rd table I just wanna join 2 columns, if a condition is met. otherwise those 2 columns should be NULL.
The following statement works, but only shows me the rows where all conditions are met. How do I display the rows from JOIN 1 if the JOIN 2 condition is not met?
SELECT
a.c_name, a.c_date, a.c_description, a.c_sid,
b.c_cID, b.type,
c.name, c.value
FROM
TABLE_1 a
INNER JOIN
TABLE_2 b
ON
(b.c_name = a.c_name)
INNER JOIN
TABLE_3 c
ON
(c.c_name = a.c_name)
AND
(c.comp_cID = b.comp_cID)
Thanks a lot and best regards,
TomOUTER JOIN
-
Update table based on values from other table
Hi,
I am trying to update one table based on the values of another table. Since you can't use From in update statements, how do you execute this?
For example i have to tables, Table A and Table B. I want to update a column or columns in Table A based on another value in Table B:
So if the column in Table B was 1 then column in Table A would be Yes, if Table B was 2, then Table A would be Yes, if Table B was 3 then Table A would be N/A and so on...
Any help would be appreciated.
thanks,
scottSQL> select * from t1;
ID ST
1
2
3
SQL> select * from t2;
NO
1
2
3
4
SQL> update t1 set status=(select decode(no,1,'Y',2,'N','NA') from t2 where t1.id=t2.no);
3 rows updated.
SQL> select * from t1;
ID ST
1 Y
2 N
3 NA
Daljit Singh -
Force Null values in a result query
considering a table X with columns NAMES, MONTH and VALOR and a table DIMDATE with columns MONTH and YEAR.
These tables have data according to the following values:
Table X
Nome Mês Valor
Joe Jan 10
Joe Fev 24
Joe Mar 30
Joe Abr 12
Joe Mai 17
Joe Jun 25
Joe Jul 30
Joe Ago 12
Joe Set 11
Mary Jan 12
Mary Fev 13
Mary Mar 25
Mary Abr 14
Mary Mai 11
Mary Jun 30
Mary Jul 32
Mary Ago 24
Mary Set 22
and Table DIMDate
Month Year
1 2012
2 2012
3 2012
4 2012
5 2012
6 2012
7 2012
8 2012
9 2012
10 2012
11 2012
12 2012
Is there any way to make a query that forces the output that shows the JOE and include MONTH OCT and VALOR = Null like as the result ?
Nome Mês Valor
Joe Jan 10
Joe Fev 24
Joe Mar 30
Joe Abr 12
Joe Mai 17
Joe Jun 25
Joe Jul 30
Joe Ago 12
Joe Set 11
JOE OCT Null
Regards
AndréExample:
SQL> select * from dept;
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 IT SUPPORT LONDON
SQL> select empno, ename, sal, deptno from emp;
EMPNO ENAME SAL DEPTNO
7369 SMITH 800 20
7499 ALLEN 1600 30
7521 WARD 1250 30
7566 JONES 2975 20
7654 MARTIN 1250 30
7698 BLAKE 2850 30
7782 CLARK 2450 10
7788 SCOTT 3000 20
7839 KING 5000 10
7844 TURNER 1500 30
7876 ADAMS 1100 20
7900 JAMES 950 30
7902 FORD 3000 20
7934 MILLER 1300 10
14 rows selected.
SQL> select d.deptno, d.dname, max(e.sal)
2 from dept d
3 left outer join emp e on (e.deptno = d.deptno)
4 group by d.deptno, d.dname
5 order by 1
6 /
DEPTNO DNAME MAX(E.SAL)
10 ACCOUNTING 5000
20 RESEARCH 3000
30 SALES 2850
40 OPERATIONS
50 IT SUPPORT
SQL>This is showing all departments with the maxium salary of the employees within that department, and it's including departments that haven't got any employees. -
How to Get the min,max and original values in a single query
Hi,
I have a task where in i have to the min , max and the original values of a data set .
I have the data like below and i want the target as well as mentioned below
SOURCE
DATASOURCE
INTEGRATIONID
SLOT_DATE
SLOT1
SLOT2
SLOT3
SLOT4
SLOT5
SLOT6
SLOT7
SLOT8
SLOT9
SLOT10
1
101
201111
100
100
200
100
100
100
300
300
300
300
1
101
2011112
200
200
200
200
100
100
100
100
200
300
TARGET
DATASOURCE
INTEGRATIONID
SLOT_DATE
SLOT_VALUE
SLOT MIN
SLOT_MAX
SLOT NUMBER
1
101
201111
100
1
2
SLOT1
1
101
201111
100
1
2
SLOT2
1
101
201111
200
3
3
SLOT3
1
101
201111
100
4
6
SLOT4
1
101
201111
100
4
6
SLOT5
1
101
201111
100
4
6
SLOT6
1
101
201111
300
7
10
SLOT7
1
101
201111
300
7
10
SLOT8
1
101
201111
300
7
10
SLOT9
1
101
201111
300
7
10
SLOT10
1
101
2011112
200
1
4
SLOT1
1
101
2011112
200
1
4
SLOT2
1
101
2011112
200
1
4
SLOT3
1
101
2011112
200
1
4
SLOT4
1
101
2011112
100
5
8
SLOT5
1
101
2011112
100
5
8
SLOT6
1
101
2011112
100
5
8
SLOT7
1
101
2011112
100
5
8
SLOT8
1
101
2011112
200
9
9
SLOT9
1
101
2011112
300
10
10
SLOT10
e
so basically i would first denormalize the data using the pivot column and then use min and max to get the slot_start and slot_end.
But then i
can get the min and max ... but not the orignal values as well.
Any thoughts would be appreciated.
ThanksIf you want to end up with one row per slot per datasource etc, and you want the min and max slots that have the same value as the current slot, then you probably need to be using analytic functions, like:
with t as
(SELECT 1 datasource,101 INTEGRATIONID, 201111 slotdate, 100 SLOT1, 100 SLOT2, 200 slot3, 100 slot4, 100 slot5, 100 slot6, 300 slot7, 300 slot8, 300 slot9, 300 slot10 FROM DUAL union all
SELECT 1, 101, 2011112, 200, 200, 200, 200, 100, 100, 100, 100, 200, 300 FROM DUAL),
UNPIVOTED AS
(SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,1 SLOT,SLOT1 SLOT_VALUE
FROM T
UNION ALL
SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,2 SLOT,SLOT2
FROM T
UNION ALL
SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,3 SLOT,SLOT3
FROM T
UNION ALL
SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,4 SLOT,SLOT4
FROM T
UNION ALL
SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,5 SLOT,SLOT5
FROM T
UNION ALL
SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,6 SLOT,SLOT6
FROM T
UNION ALL
SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,7 SLOT,SLOT7
FROM T
UNION ALL
SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,8 SLOT,SLOT8
FROM T
UNION ALL
SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,9 SLOT,SLOT9
FROM T
UNION ALL
SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,10 SLOT,SLOT10
FROM T)
select DATASOURCE,INTEGRATIONID,SLOTDATE,slot,slot_value,min(slot) OVER (partition by datasource,integrationid,slotdate,rn) minslot,
max(slot) OVER (partition by datasource,integrationid,slotdate,rn) maxslot
FROM
select DATASOURCE,INTEGRATIONID,SLOTDATE,max(rn) over (partition by datasource,integrationid,slotdate order by slot) rn,slot,slot_value
FROM
(SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,slot,slot_value,
case when row_number() over (partition by datasource,integrationid,slotdate order by slot) = 1 or
lag(slot_value) over (partition by datasource,integrationid,slotdate order by slot) <> slot_value
then row_number() over (partition by datasource,integrationid,slotdate order by slot)
ELSE null
END rn
from unpivoted
order by DATASOURCE,INTEGRATIONID,SLOTDATE,slot -
SELECT :Selecting multiple values through a single query
I have to select 2 distinct column values into variables in one query.Something like
SELECT TSTAMP into varConcurrencyTstamp,VoucherTbId INTO cnt
FROM VOUCHERTB
WHERE
VOUCHERTBID =FnDeleteVOUCHERTB.VOUCHERTBID;//Not working
This task can be done by 2 separate queries as follows
SELECT COUNT(*) INTO cnt
FROM VOUCHERTB
WHERE
VOUCHERTBID =FnDeleteVOUCHERTB.VOUCHERTBID;
SELECT TSTAMP
INTO varConcurrencyTstamp
FROM VOUCHERTB
WHERE VOUCHERTBID = FnDeleteVOUCHERTB.VOUCHERTBID;
.Is there a way to do it in one shot something similar to the query first given...SELECT TSTAMP, count(*) into time_Stamp, cnt_val
FROM VOUCHERTB
WHERE VOUCHERTBID = FnDeleteVOUCHERTB.VOUCHERTBID
group by TSTAMP
/ -
Tricky SQL query... how to get all data in a single query?
create table employee_definition (def_id number, def_name varchar(50));
insert into employee_definition values (100, 'EMAIL');
insert into employee_definition values (200, 'MOBILE_PHONE');
insert into employee_definition values (300, 'HOME_PHONE');
SQL> select * from employee_definition;
DEF_ID DEF_NAME
100 EMAIL
200 MOBILE_PHONE
300 HOME_PHONE
create table employee_data (def_id number, def_value varchar(20), emp_id number);
insert into employee_data values (100, '[email protected]', 123);
insert into employee_data values (200, '01232222', 123);
insert into employee_data values (300, '5555', 123);
insert into employee_data values (100, '[email protected]', 666);
insert into employee_data values (200, '888', 666);
insert into employee_data values (300, '999', 666);
insert into employee_data values (300, '444', 777);
SQL> select * from employee_data;
DEF_ID DEF_VALUE EMP_ID
100 [email protected] 123
200 01232222 123
300 5555 123
100 [email protected] 666
200 888 666
300 999 666
300 999 777
7 rows selected.I'm supposed to create a SQL that will return me the email, mobile_phone, and home_phone for a set of employees. The result will be something like this:
EMPLOYEE ID | HOME_PHONE | MOBILE_PHONE | EMAIL
123 | 5555 | 01232222 | [email protected]
666 | 999 | 888 | [email protected]
777 | 444 | null | nullThe thing I'm finding difficulty here is that the same column is used to store different values, based on the value in employee_definition table (something like a key/value pair). If I do:
SQL> select emp_id, def_value as email from employee_data, employee_definition
2 where employee_data.def_id = employee_definition.def_id
3 and employee_definition.def_name = 'EMAIL';
EMP_ID EMAIL
123 [email protected]
666 [email protected]'s partially ok.. I'm just getting the definition for 'EMAIL'. But how can I get all the values in a single query, knowing that the column stores different values based on def_name?Oh no, not again.
Entity attribute models always seem like a great idea to people who have been in the profession for five minutes and lack any kind of fundamental knowledge.
It staggers me that someone with 2,345 posts still believes "you need a 'detail table' for [storing multiple telephone numbers]"
"A person can have multiple telephone numbers" is not an excuse to build a tired person_attribute table. Niether is the bizarre proposal by someone with over 4k posts who should know better in an earlier post that EAV models are necessary to support temporal fidelity.
Taken to it's logical conclusion, EAV modelling leads to just two application tables. THINGS and THING_ATTRIBUTES. And when you consider that a THING_ATTRIBUTE is also a THING, why not roll those two tables up into one also? Hmmm, what does THINGS and THING_ATTRIBUTES look like? I know, TABLES and COLUMNS. Who would've guessed? SQL already provides the completely flexible extensible attribute model the advocates of EAV proscribe. But it also has data types, physical data independence, constraints and an efficient query language which EAV does not.
EAV modelling errodes the semantics of the attributes which are bundled into the "attribute" table.
There is no point in storing 12 different phone numbers with implied functional dependency to unconstrained and often repeating notional attributes like "MOBILE", "LANDLINE", "WORK", err, "WORK2", err, "MOBILE2", err, ... when this phone type attribute has no semantic value. When you want to call someone, you invariably want to retrive the prefered_phone_number which may depend on a time of day, or a call context.
These things need to be modelled properly (i.e normalised to BCNF) within the context of the database.
Maybe you are looking for
-
How to create a .gif slide show? I've currently animated a slide show as a quicktime movie (with hidden controller, autoplay & looped) which works pretty well. But I guess a .gif would be quicker and more universal? What software makes a .gif? Thanks
-
Using SwingConstants in NetBeans designer mode
In NetBeans, when adding property get/set methods for a bean they'll be added automatically to the properties panel in the designer. I've written a setHorizontalAlignment method for a custom JComboBox I use and I want to use SwingConstants as the met
-
Hi I am trying to update my iphone from software version 3.1.3 to the latest one. Itunes says mine phone is backing up.. I have left it over night and its still is backing up. Not sure what to do.. Help!!
-
What is the best approach to process data on row by row basis ?
Hi Gurus, I need to code stored proc to process sales_orders into Invoices. I think that I must do row by row operation, but if possible I don't want to use cursor. The algorithm is below : for all sales_orders with status = "open" check for credit l
-
Tetris always starts with the same blocks
Has anyone else noticed that the first game in Tetris after launching it always starts with the same blocks? For me, it always gives me the red "Z" block, the yellow square, the green "Z", orange "L", purple "T", blue straight block, blue "L", and so