oracle - SQL*PLUS Concatenation Issues -


this question has answer here:

this sql*plus

i have table "patient" has 5 fields.

create table patient (pat_id         char (4) primary key,  pat_name       varchar (7),  admitted       date,  room           char (3),  doctor         varchar (7));  insert patient values (1001, 'fred', '30-mar-07', 101, 'payne'); 

i want create view output this:

patient      doctor       room         admitted     "-----------------------------------------------" 1001 fred    payne        101      march 31, 2007 

the problem want concatenate pat_name , pat_id own separate "patient" column also have other 3 fields concatenated it(notice there no separation in 'dashes').

basically, have 5 fields. simplicity's sake, let's call them fields "1, 2, 3, 4, 5".

i want combine of them together, first, need combine fields 1 , 2. small column (fields 1 , 2 combined) called "patient". then, need concatenate fields 3, 4, , 5 patient column 5 fields concatenated 4 headings.

you concatenate expressions oracle || concatenation operator.

to spacing, can use rpad function pad out expressions specified length (or truncate them specified length if longer).

col myline heading "patient      doctor       room         admitted"  select rpad(p.pat_id,5)||rpad(p.pat_name,8)||rpad(p.doctor,13)        ||rpad(p.room,4)||to_char(p.admitted,'month dd, yyyy')        myline   patient p  order p.pat_id 

the date value show on line formatted as

march     31, 2007 

(with space in month name, oracle reserves space longest month name 'september' (?) in format model.


you may need add sql*plus directive specify length reserved display of column, e.g.:

col myline format a48 

if work looks this:

patient      doctor       room   admitted     ------------ ------------ ------ ------------------ 1001 fred    payne        101    march     31, 2007 

then this:

col patient  format a13 col doctor   format a13 col room     format a6 col admitted format a21  set lines 41 set feedback off  select rpad(p.pat_id,5)||rpad(p.pat_name,8) "patient"      , rpad(p.doctor,13) "doctor"      , rpad(p.room,6) "room"      , to_char(p.admitted,'month dd, yyyy') "admitted"   patient p  order p.pat_id 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -