Tuesday, June 18, 2019

py0019 : Color code in idel Python Shell

When you write a code in IDLE python shell, you may observe 4 different colors, yes these colors have some meaning:


  • Purple: It shoe that you have typed a command in python shell
  • Green: Some content sent to command
  • Blue: Any output from command will be BLUE in color
  • Black: not a command


What are commands: Print/quit are commands in python. Command generally written with round parenthesis i.e. print(), quit()

Monday, June 17, 2019

Function to Find Valid Invalid Date Format Oracle

Some times we need to check data inside the column if it is in proper shape or proper format. Like some of database saving date data in side of varchar field. which is wrong, if could have invalid date format or can have bad data in terms of date, month, year.

Here i am going to provide you the example of function to check date is properly saved in varchar column, regardless of any date format. Function will check for all combination of date type format and will return valid or invalid i.e. atleast one format is correctly saved in column.

create function datetype1(d varchar2) return varchar2
is
  v_date date;
begin
  select to_date(d,'DD-MON-YYYY') into v_date from dual;
  return 'Valid';
  exception when others then return 'Invalid';
end;
/

create function datetype2(d varchar2) return varchar2
is
  v_date date;
begin
  select to_date(d,'DD-MON-YY') into v_date from dual;
  return 'Valid';
  exception when others then return 'Invalid';
end;
/

create function datetype3(d varchar2) return varchar2
is
  v_date date;
begin
  select to_date(d,'DD-MM-YYYY') into v_date from dual;
  return 'Valid';
  exception when others then return 'Invalid';
end;
/

create function datetype4(d varchar2) return varchar2
is
  v_date date;
begin
  select to_date(d,'MM-DD-YYYY') into v_date from dual;
  return 'Valid';
  exception when others then return 'Invalid';
end;
/

create function datetype5(d varchar2) return varchar2
is
  v_date date;
begin
  select to_date(d,'YYYY-MM-DD') into v_date from dual;
  return 'Valid';
  exception when others then return 'Invalid';
end;
/

create function datetype6(d varchar2) return varchar2
is
  v_date date;
begin
  select to_date(d,'DD/MM/YYYY') into v_date from dual;
  return 'Valid';
  exception when others then return 'Invalid';
end;
/

create function datetype7(d varchar2) return varchar2
is
  v_date date;
begin
  select to_date(d,'MM/DD/YYYY') into v_date from dual;
  return 'Valid';
  exception when others then return 'Invalid';
end;
/

create function datetype8(d varchar2) return varchar2
is
  v_date date;
begin
  select to_date(d,'YYYY/MM/DD') into v_date from dual;
  return 'Valid';
  exception when others then return 'Invalid';
end;
/



CREATE OR REPLACE Function DateFieldValidInvalid
   ( field_v in varchar2)
   RETURN varchar2
IS
   cnumber varchar2(2000);
v_query1 varchar2(4000);

BEGIN
v_query1 := 'select a from (select datetype1(''' || field_v||''') a from dual union ' ||
'select datetype2(''' || field_v||''') from dual union ' ||
'select datetype3(''' || field_v||''') from dual union ' ||
'select datetype4(''' || field_v||''') from dual union ' ||
'select datetype5(''' || field_v||''') from dual union ' ||
'select datetype6(''' || field_v||''') from dual union ' ||
'select datetype7(''' || field_v||''') from dual union ' ||
'select datetype8(''' || field_v||''') from dual) where a = ''Valid''' ;
 
execute immediate v_query1 into cnumber;

RETURN cnumber;
END;


select DateFieldValidInvalid(created_ON) from t_contact ;

Friday, June 14, 2019

Row Count of Each Table

Is it easier way to find row count of each table in SQLSERVER 

Yes, earlier i have added post which is custom dynamic tsql to get row count of each table in sqlserver. Even SQLServer system tables itself store the result of each row count of all tables of all databases.

It can be easily identify using sys.tables and partition table. This is not a innovative but it is just a smartness at your work



SELECT 
SCHEMA_NAME(schema_id),
t.name AS Table_Name,
SUM(p.[rows]) AS [TotalRowCount]
FROM 
sys.tables t
JOIN sys.partitions p ON t.object_id = p.object_id
AND p.index_id IN ( 0, 1 )
GROUP BY SCHEMA_NAME(schema_id), t.name
having 
SUM(p.[rows]) > 1

to get all tables remove having clause from above query you will be able to see the result of each table. Right now it is returning the only those tables whose result is more than 1.

Friday, June 7, 2019

print row count in begin try on delete

I was executing below section of code on sqlserver

SET NOCOUNT ON
BEGIN TRY
BEGIN TRAN

select count(*) from my_table

delete from my_table where key = 1

COMMIT
PRINT 'Success!'
END TRY
BEGIN CATCH
ROLLBACK
PRINT 'Fail'+ ERROR_MESSAGE()
END CATCH
SET NOCOUNT OFF

Here my code is able to show the count of Select query but not able to show the result of delete query in result window.

Requirement:
To show the result of delete in query window.

Solution :
Remove SET NOCOUNT ON and SET NOCOUNT OFF from your block, you will be able to see the delete result.

Count of Each Table Oracle

small and simple plsql script is created to find count of each table in oracle database for one schema.
This PLSQL can execute either on SQL Developer or SQLPlus editor. It's a small plsql used cursor and for loop and concept of dynamic query execution in Oracle.

See below code

set serveroutput on
declare
    
    cursor c2 is select * from user_tables;
    v_sql1 varchar(4000);
    v_count number;
begin
   for ii in c2 loop
    v_sql1 := 'select count(*) from '|| ii.table_name;
    
    execute immediate v_sql1 into v_count;
    dbms_output.put_line(ii.table_name ||'-'|| v_count);
   end loop;
end;
/

Simple Cursor and For Loop in Oracle

in oracle we can open cursor and need to start loop (while similar as SQL Server) so that we can process data what ever is coming in cursor.

In this post i am going to show you simplest code or plsql to do cursor and for loop of cursor.
See below syntax

set serveroutput on
declare
    cursor c2 is select * from user_tables;
begin
   for ii in c2 loop
    dbms_output.put_line(ii.table_name);
   end loop;
end;
/


In this cursor Oracle will automatically open and close the cursor before start and end of the program. so do not worry about the having 'open cursor' and 'close cursor' in your code.

Tuesday, June 4, 2019

py0018 : Change color of button on hover and borderless button

while working in python we might need to create border less button or change the color of button on hover.
for this we can add parameter in method (def) of button, activebackground='green', borderwidth=0. Activebackground will be responsible of change color of button when mouse cursor hover on the the button
and borderWidth will remover the border from the button

import tkinter as tk

class HoverButton(tk.Button):
    def __init__(self, master, **kw):
        tk.Button.__init__(self,master=master,**kw)
        self.defaultBackground = self["background"]
        self.bind("Add**less**than**Enter**Add**greater**than", self.on_enter)
        self.bind("Add**less**than**Leave**Add**greater**than", self.on_leave)

    def on_enter(self, e):
        self['background'] = self['activebackground']

    def on_leave(self, e):
        self['background'] = self.defaultBackground

root = tk.Tk()

classButton = HoverButton(root, text="Classy Button", activebackground='green', borderwidth=0)
classButton.grid()

root.mainloop()

Note* Add**less**than** *** **Add**greater**than
please modify the code before execution, add less than and greater than sign

Monday, June 3, 2019

py0017 : Parameter 'bg' value is not used less... (ctrl+F1)

Error:
Parameter 'bg' value is not used less... (ctrl+F1)
Inspection infoL This inspection highlights local variablem parapmeters or local function unused in scope

Requirement:
How to set default color of button default to white

Solution:
Used "bg" paramter to set color value

   def numrcbtn(self, val, write=True, width=12, height=4, padx=1, pady=1) :
   return HoverButton(root, text=val, command=lambda: self.click(val, write), width=width, height=height, padx=padx, pady=pady, activebackground='Gainsboro' , bg='white')
   
   
web stats