Search This Blog

Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

Wednesday, January 2, 2013

Change a existing column type (varchar to clob) usig SQL query .

Problem : Change a column type (varchar to clob) usig SQL query .
 
Solution : To change column type column should be dropped and then craeted again .. then if it contains data then risk to loose that .
To prevent it we can use following way : 
1.  Add a temp column :
ALTER TABLE TEST ADD temp VARCHAR2(4000);
2. copy data to temp column :
UPDATE TEST
   SET temp = DBMS_LOB.SUBSTR (a, 4000),
       a = NULL;
3. drop the original column "A"
ALTER TABLE TEST DROP COLUMN A;

4. Either rename the column "temp" to original "A"  ((if just modifying size)
ALTER TABLE TEST RENAME COLUMN temp TO A;

Optional  step
5. create column "A" as clob and repeat step 2 to copy data from "temp" to "A" and after copy remove "temp"

cheers

Kapil

Thursday, March 10, 2011

Power of case statement into SQL - Multiple result in Single Query

Query Statement : Having a table "Acct_stat" with column "Trans_type"(Dr or Cr) , "Amount" Pls write a single query which should return sum of "Debit" , "Credit" and "Total Balance" of each account.

Answer : We can use a case statement as best choice for such queries like following with group by Account:

Let's go with following SQL

Drop table Acct_stat;
create table Acct_stat (Acount_No varchar2(10),TRANS_TYPE VARCHAR2(2),AMOUNT NUMBER);

insert into Acct_stat values('1' , 'DR' , 100);
insert into Acct_stat values('1' , 'CR' , 100);
insert into Acct_stat values('1' , 'DR' , 100);
insert into Acct_stat values('2' , 'CR' , 100);
insert into Acct_stat values('2' , 'CR' , 100);
insert into Acct_stat values('2' , 'DR' , 100);
insert into Acct_stat values('3' , 'DR' , 100);
insert into Acct_stat values('3' , 'CR' , 100);

SELECT Acount_No ,
SUM (CASE WHEN Trans_type = 'DR'
THEN AMOUNT ELSE null END) DB_SUM,
SUM (CASE WHEN Trans_type = 'CR'
THEN AMOUNT ELSE null END) CR_SUM,
SUM (AMOUNT) TOT_BAL
FROM ACCT_STAT GROUP BY (Acount_No);

and Result is :
Acount_No DB_SUM CR_SUM TOT_BAL
1 200 100 300
2 100 200 300
3 100 100 200


Cheers

Kapil

Friday, September 3, 2010

How to insert "&" inside database table

Following SQL is to insert "&" into the database

Insert into table_nm (LINK_NAME) values ('Gold ' || chr(38) || ' Notes');

Value will come as "Gold & Notes" in column.

Cheers

Thursday, February 11, 2010

Create database sequence in cyclic manner

I need to recycle the value in the database after reaching @ max count .. for that planned to use oracle sequences and found that following way we can recycle the sequence value from the start :

DROP SEQUENCE sms_job_seq;
CREATE SEQUENCE sms_job_seq
MINVALUE 1
MAXVALUE 100
START WITH 1
INCREMENT BY 1
CYCLE
NOCACHE;

we have to set cycle property during sequence creation .. then after reaching max limit .. (100 here) next value will start from 1. (No need of application coding to do this :) )

Cheers..

Popular Posts