What is the syntax for decode() function of oracle to encrypt string in
Example :- if i want to encrypt 'suvendu' with'***' and 'mohan' with '$$$' for column fname of samples table
desc samples
Name Null Type
------ -------- ------------
EMP_ID VARCHAR2(20)
LNAME CHAR(10)
FNAME CHAR(20)
DEPT CHAR(20)
SAL NOT NULL NUMBER(12,2)
H_DATE DATE
EMAIL VARCHAR2(20)
DESG VARCHAR2(25)
While you can use DECODE to handle this, as in:
SELECT DECODE(FNAME,
'suvendu', '***',
'mohan', '$$$',
FNAME) AS DERIVED_COL
FROM SAMPLES
IMO using a CASE expression is a better choice
SELECT CASE FNAME
WHEN 'suvendu' THEN '***'
WHEN 'mohan' THEN '$$$'
ELSE FNAME
END AS DERIVED_COL
FROM SAMPLES
as it makes it clearer what's going on and is easier to read.
Best of luck.
Updated(try this):
SELECT
DECODE(fname,'suvendu', '***', 'mohan", '$$$') AS F_NAME
--add other columns/column-list here if needed
FROM
samples
The line starting with -- is a commented line
Related
create type emp_t AS OBJECT(
EMPNO CHAR(6),
FIRSTNAME VARCHAR(12),
LASTNAME VARCHAR(15),
WORKDEPT REF dept_t,
SEX CHAR(1),
BIRTHDAY DATE,
SALARY NUMBER(8,2)
)
/ <--------- this one
As the comment said, that is used as a Delimiter, but it has to be assigned first.
The normal delimiter is (;), you can set a new one by adding the command and then the sign you want to use.
Once change the delimiter, you can use the new delimiter to end a statement as follows:
DELIMITER //
SELECT * FROM customers //
SELECT * FROM products //
Here is some Documentation.
I have a following query description. How to express it in SQL?
Query -> list all the employee first name such that their emp_pct is less than any of the employee whose proj_num is 18 (Make use of ANY).
I have following table name EMP_2;
Name Type
-----------------------------------
EMP_NUM CHAR(3)
EMP_LNAME CHAR(15)
EMP_FNAME CHAR(15)
EMP_INITIAL CHAR(1)
EMP_HIREDATE DATE
JOB_CODE CHAR(3)
EMP_PCT NUMBER(5,2)
PROJ_NUM CHAR(3)
Here you go. The ANY keyword compares and short circuits the moment it finds a matching row in the sub-query.
SELECT a.EMP_FNAME
FROM EMP_2 a
WHERE a.EMP_PCT < ANY (
select EMP_PCT
from EMP_2
where EMP_NUM <> a.EMP_NUM and PROJ_NUM = 18);
INSERT INTO
FLAG ("OPT_FLAG_KEY","H_KEY","FIRST_NAME","LAST_NAME",
"MIDDLE_NAME","TITLE","CREDENTIALS","ADDRESS_LINE_1",
"ADDRESS_LINE_2","ADDRESS_LINE_3","CITY","STATE",
"POSTAL_CODE","PHONE_NUMBER","BUSIN_PHONE","DECEASED",
"OPT_FLAG","OPT_FLAG_DATE","SOU_KEY","SOU_FILE_ID",
"SOU_FILE_ID_TEXT","BAT_ID","PHONE_NUMBER_SOURCE","BIRTH_DATE")
VALUES(37009326,4,'Daniel','Boyle',NULL,NULL,NULL,'368 Road',
NULL,NULL,'Doylown','BVBV',1801,NULL,NULL,'NO','OUT',
TO_CHAR('10-AUG-16','DD/MM/YYYY'),201,
TO_DATE(SUBSTR('vhic_pavir_20160810.txt',12,8),YYYYMMDD),
'2016-08-10',598441,NULL,TO_DATE('03-FEB-1952',DD-MM-YYYY));
I have run this query in my DB am getting some error like below
Error at Command Line : 1 Column : 698
Error report
SQL Error: ORA-00984: column not allowed here
00984. 00000 - "column not allowed here"
*Cause:
*Action:"
Edit: this is my desc of my table:
Name Null Type
------------------- ---- -------------
OPT_FLAG_KEY NUMBER(14)
H_KEY NUMBER(14)
FIRST_NAME VARCHAR2(50)
LAST_NAME VARCHAR2(50)
MIDDLE_NAME VARCHAR2(50)
TITLE VARCHAR2(50)
CREDENTIALS VARCHAR2(50)
ADDRESS_LINE_1 VARCHAR2(100)
ADDRESS_LINE_2 VARCHAR2(100)
ADDRESS_LINE_3 VARCHAR2(100)
CITY VARCHAR2(50)
STATE VARCHAR2(20)
POSTAL_CODE VARCHAR2(20)
PHONE_NUMBER VARCHAR2(100)
BUSIN_PHONE VARCHAR2(100)
DECEASED VARCHAR2(5)
OPT_FLAG VARCHAR2(10)
OPT_FLAG_DATE DATE
SOU_KEY NUMBER(14)
SOU_FILE_ID DATE
SOU_FILE_ID_TEXT VARCHAR2(20)
BATCH_ID NUMBER(14)
PHONE_NUMBER_SOURCE VARCHAR2(100)
BIRTH_DATE DATE
There are several issue with your statement. First, double-quotes are not required on column names (unless you have mixed cases or space), remove them.
TO_CHAR('10-AUG-16','DD/MM/YYYY') -> 10-AUG-16 is a string, not a date. Thus it does not make any sense to convert a string to a string
TO_DATE(SUBSTR('vhic_pavir_20160810.txt',12,8),YYYYMMDD) -> Format must be in single-quotes, i.e. TO_DATE(SUBSTR('vhic_pavir_20160810.txt',12,8),'YYYYMMDD')
'2016-08-10' -> do you like to insert a DATE value or a string? You provided a string, not a date.
TO_DATE('03-FEB-1952',DD-MM-YYYY)) -> Format must be in single-quotes, see above. MM means the month number, not the month name.
I am trying to describe a table without using the DESCRIBE command but I want to combine the query with a substitution variable. Assuming I have the following table:
--DROP TABLE customers CASCADE CONSTRAINTS PURGE;
CREATE TABLE customers
( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50)
);
Following the posts here and here but adding a substitution variable, I have the following:
ACCEPT myv CHAR PROMPT 'Enter a table name: '
SELECT
column_name AS "Name",
nullable AS "Null?",
concat(concat(concat(data_type,'('),data_length),')') AS "Type"
FROM user_tab_columns
WHERE table_name = '&myv';
This returns a blank table with the appropriate column names. It doesn't matter if I entered the table name in the input prompt as CUSTOMERS or customers. However, desc customers yields:
Name Null Type
------------- -------- ------------
CUSTOMER_ID NOT NULL NUMBER(10)
CUSTOMER_NAME NOT NULL VARCHAR2(50)
CITY VARCHAR2(50)
Any idea how I can get substitution variable to work here? Thanks.
I got it to work with a bind variable. Not exactly sure what is going on because #kordirko said the query worked for him as is. Anyway, to get it to work for me (I'm using SQL Developer Version 4.0.3.16), I used bind variable, like so:
SELECT
column_name "Name",
nullable "Null?",
concat(concat(concat(data_type,'('),data_length),')') AS "Type"
FROM user_tab_columns
WHERE table_name = :myv;
I then entered CUSTOMERS into the value field of the Enter Binds window and the query executed fine. If anybody knows why substitution variable failed but bind variable did not, that will certainly add to the discussion.
I have installed Oracle 10g in my virtual XP and created a table using
create table reg1 (
fname varchar2(30),
lname varchar2(30),
addr varchar2(30),
mail varchar2(30),
occu varchar2(30),
uname varchar2(30),
passwd varchar2(30)
);
and the table created successfully.But when I am trying to fetch the values by simple query like
select fname, lname
from reg1
where uname="bbb";
I am getting error like
ORA-00904: "bbb": invalid identifier
I cannot understand what I have done wrong here.
Use single quotes.
select fname,lname from reg1 where uname='bbb';
Oracle uses double quotes " in order to identify cased object names. For instance the table "test" is not the same as the table test.
Strings should be enclosed by single quotes, '.
Making your query:
select fname, lname from reg1 where uname = 'bbb';
What's actually happening in your query is Oracle is trying to find the column "bbb" in the table reg1, as this column doesn't exist you get the error thrown.