DECODE with NULL values - sql

I want to insert ZEROS or Spaces when the value is NULL
I tried for small column length and it worked:
INSERT INTO ABCTABLE (COLUMN1)
VALUES ('DECODE(MDSE_CD,NULL,''000'',LPAD(TO_NUMBER(MDSE_CD,''3'','' ''))');
But how do I execute the second query that has around 400 column length:
INSERT INTO ABCTABLE (COLUMN1)
VALUES ('DECODE(MDSE_CD,NULL,''see notes below'',LPAD(TO_NUMBER(MDSE_CD,''400'','' ''))');
Note:How do I insert 400 zeros or spaces when the column is NULL

CREATE TABLE my_test( val VARCHAR2(500));
INSERT INTO my_test
SELECT TO_CHAR(NVL(TO_CHAR(val), rpad(NVL(val,'0'), 400, '0')))
FROM
(WITH t(val) AS
(SELECT '1' FROM dual
UNION
SELECT NULL FROM dual
)
SELECT * FROM t
);
SELECT * FROM my_test;
val
--
1
00000

Because of the DECODE I'm assuming Oracle...
To get 400 zeros in Oracle:
LPAD('0', 400, '0')
The third parameter is what to pad with; in this case zeros.
To get 400 spaces in Oracle:
LPAD(' ', 400)
Since the strings are all the same character you could also use RPAD.

Related

How can i filter out the Japanese values in an SQL Query?

I am querying from an Oracle 12c database and i need to filter out values which are in Japanese because the Java application receiving these values currently do not have the ability to display Japanese characters. For a quick demo, is there any way we can avoid the Japanese strings from the results?
You will need to replace all Non English characters with NULL for all the columns you have in your select statement.
select regexp_replace (column_name,'[^\x80-\xFF]',NULL) from table_name;
This will replace anything which is not in English characters to NULL
Given the unicode ranges for Japanese characters you can just replace the Japanese characters:
SELECT REGEXP_REPLACE(
your_column,
UNISTR( '[\3000-\303f\3040-\309f\30a0-\30ff\ff00-\ffef\4e00-\9faf]' ),
NULL
)
FROM your_table
or ignore those rows:
SELECT *
FROM your_table
WHERE NOT REGEXP_LIKE(
your_column,
UNISTR( '[\3000-\303f\3040-\309f\30a0-\30ff\ff00-\ffef\4e00-\9faf]' )
)
I did not altered the NLS_LANG format to insert the Japanese character properly. Using regexp_like you can filter records which don't have alphanumeric data.
create table table1 (name varchar2(100));
insert into table1 (name) values ('ABC');
insert into table1 (name) values ('DEF');
insert into table1 (name) values ('GHI');
insert into table1 (name) values ('JKL');
insert into table1 (name) values ('GHI');
insert into table1 (name) values ('昨夜のコンサ');
insert into table1 (name) values ('昨夜のABC');
select * from table1;
select * from table1 where regexp_like (name,'^[0-9a-zA-Z]+$');
select nvl(regexp_replace(name,'[^0-9a-zA-Z'']',''),'blank') from table1;

Insert 1000 rows in single sql statment or procedure

How should i write a single sql statement or a stored procedure,
To insert 1000 values in 1000 rows and same column with each column having different values (among those 1000)
Here is the query i wrote,
INSERT INTO a_b values
(
(SELECT max(a_b_id) + 1 from a_b),
1111,
(SELECT s_id FROM a_b WHERE s_id in ('0','1','2','3','4')),
0,
1,
sysdate,
sysdate,
0,
1,
null
);
like say, i have 1000 s_id's i want select them one by one and insert them in one particular column, each time creating a new row.
EX, in first row s_id should be 0 then in second row it should be 1 like that goes on till thousand, attached an image of sample database i am working with.
You can use connect by for this:
INSERT INTO a_b (s_id, col2, col3, ....)
select level, --<< this value will change for every row
1111,
sysdate,
... more columns ...
from dual
connect by level <= 1000;
you can use cross apply to get 1000 rows along with 1000 other columns to insert 1000 rows as below:
insert into a_b (column names...)
select (max(a_b_id) over()) +1 as MaxId, s_id from a_b a cross apply (select 0, 1,SYSDATETIME, SYSDATETIME, 0, 1, null) b where a.s_id('111','222')--condition
The below is a syntax error. You will never get something like that to work.
create table fff
( id int not null
);
insert fff values (select 1,7777,select 3, select 3);
So you need to break it up into chunks
DROP PROCEDURE IF EXISTS uspRunMe;
DELIMITER $$
CREATE PROCEDURE uspRunMe()
BEGIN
insert into a_b select max(a_b_id) + 1 from a_b;
insert into a_b values (1111);
insert into a_b SELECT s_id FROM a_b WHERE s_id in ('0','1','2','3','4');
insert into a_b values (0,1);
insert into a_b select sysdate,sysdate;
insert into a_b values (0,1,null);
END;$$
DELIMITER ;
Test it:
call uspRunMe();
The above is for MySQL. You have a few db engines tagged here.

sql code to convert varchar colum to int

I am stuck on converting a varchar column schedule containing the following data 0, 1, 2, 3,4,8,9,10,11,12,15,16,17,18,19 to INT. I know, please don't ask why this schedule column was not created as INT initially, long story.
So I tried this, but it doesn't work. and give me an error:
select CAST(schedule AS int) from shift_test:
the query should check if the numbers representing days are found in the schedule filed using the sql code below
select empid, case when ((DateDiff(hour,'01-01-2014 07:00' , '01-01-2014 00:00')/ 24 )% 15) in ( CAST(schedule AS int))
then 'A' else '*' end as shift_A from Shift_test
After executing i get this error.
Conversion failed when converting the varchar value to int.
Any help will be appriciated
Use ISNUMERIC() test if you are using version 2008 or 2008R2. In SQL SERVER 2012 you can use TRY_CAST() function, which checks if the data conversion is allowed for given literal.
Mock up code for SQL Server 2008/R2:
Select col1, col2,
case
when <condition> and isnumeric(col2) then cast(col2 as int)
else <do whatever...>
end
as converted_col2
from <yourtable>;
For SQL Server 2012:
Select col1, col2,
case
when <condition> then try_cast(col2 as int)
else <do whatever...>
end
as converted_col2
from <yourtable>;
Example with SQl Server 2008
declare #T table (empid int, schedule varchar(2)) ;
insert into #T(empid, schedule) values (1, '1');
insert into #T(empid, schedule) values (2, '2');
insert into #T(empid, schedule) values (3, '03');
insert into #T(empid, schedule) values (4, '4');
insert into #T(empid, schedule) values (5, '05');
insert into #T(empid, schedule) values (6, 'A');
select empid,
case
when ISNUMERIC(schedule) = 1
and ((DateDiff(hour,'01-01-2014 07:00' , '10-01-2014 00:00')/ 24 )% 15)
in ( CAST(schedule AS int)) then 'A'
else '*'
end
as shift_A
from #T;
# Binaya Ive added my code for more help.
The schedule column contain (0, 1, 2, 3,4,8,9,10,11,12,15,16,17,18,19) which is varchar.
i want to output A if after this calculation ((DateDiff(hour,'01-01-2014 07:00',startdate)/ 24 )% 15) the result is found in the schedule column else if after the calculation and the result is not found output *
;with Shift_runover (shift_code,schedule,endd,startdate)
-- Start at the beginning of shift.
as
(select shift_code,schedule,Cast(end_date as DateTime) as endd,Cast(start_date as DateTime)as startdate from dbo.Shift_test
union all
-- Add hours up to the desired end date.
select shift_code,schedule,endd,DateAdd(hour, 1,startdate)from Shift_runover where startdate<=endd),
Extendedsamples as
(
-- Calculate the number of days since the beginning of the first shift on 1/1/2014.
select shift_code,schedule,startdate,DateDiff(hour,'01-01-2014 07:00',startdate)/ 24 as Days from Shift_runover ),
Shifts as
(
-- the schedule column contain (0, 1, 2, 3,4,8,9,10,11,12,15,16,17,18,19) which is varchar.
-- i want to output A if ((DateDiff(hour,'01-01-2014 07:00',startdate)/ 24 )% 15) is found in the schedule colume
select *,
case when (DateDiff(hour,'01-01-2014 07:00',startdate)/ 24 )% 15 in(schedule)
then 'A' else '*' end as shift_A
from ExtendedSamples
)
select *
from Shifts
option ( maxrecursion 0 )

remove single character from sentence

I have the following table and data:
1) i want to show only the last part of sentence.
2) remove any single character from end of sentence.
3) remove and special charachter like -,#,?,_ from end of words
create table t1 (id number(9) , words varchar2(20));
insert into t1 values(1,'hello UK');
insert into t1 values(2,'hello Eypt');
insert into t1 values(3,'hello ALL');
insert into t1 values(4,'hello I');
insert into t1 values(5,'hello USA');
insert into t1 values(6,'hello #');
insert into t1 values(7,'hello #');
insert into t1 values(8,'hello A');
insert into t1 values(9,'hello 20');
insert into t1 values(10,'hello 2-2-2010');
i have used this
select REGEXP_SUBSTR(words,'\S+$)from t1;
expected results
id word
1 UK
2 EGYPT
3 ALL
5 USA
9 20
10 2-2-2010
MySQL version
SELECT id, SUBSTRING_INDEX('hello UK', ' ', -1) as word WHERE LENGHT(word) > 1
OracleDB version (The one you must use)
SELECT id, SUBSTR('hello UK', INSTR('hello UK', ' ')) as word WHERE LENGHT(word) > 1
will return in both cases {id} : UK
Don't forget to replace 'hello UK' with the good column name :)
HERE is the explanation for SUBSTR used with INSTR
Good luck :)
Read your database manual, they all have functions that do string manipulation and they all depend on the application for their syntax. SUBSTRING(words, 7, DATALENGTH(word) -7) would work in SQL Server.

Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '('

I am trying to query to insert in a table and i keep getting this message:
The Query is:
INSERT INTO A_USER(PK,status,login_id,HASHBYTES('md5', password),fk_role,last_update_ts,last_update_by,created_by)
VALUES (2,1,'abc', 'abc',2,'3/15/2012 12:21:46 PM','abc','abc')
INSERT INTO EMP_USER(PK,status,login_id,password,fk_role,last_update_ts,last_update_by,created_by)
VALUES (2,1,HASHBYTES('md5', password),'abc','abc',2,'3/15/2012 12:21:46 PM','abc','abc')
insert statement works like that
insert into table (col1, col2) values (val1, val2)
Put HASHBYTES('md5', password) in the values part and name that column in the column part
Your issue is with this line HASHBYTES('md5', password), you want to use the HASHBYTES in the VALUES area of your INSERT.
INSERT INTO A_USER
(
PK
,status
,login_id
,[password] -- change to the name of your column.
,fk_role
,last_update_ts
,last_update_by
,created_by
)
VALUES
(
2
,1
, 'abc'
,HASHBYTES('md5', 'abc') -- place your password to be hashed where 'abc' is.
,2
,'3/15/2012 12:21:46 PM'
,'abc'
,'abc'
)