Monday, November 30, 2020

, , ,

date difference function in sql with example program

  •  datediff sql function will helps to calculate the difference between two dates in week,year,months etc.
  • datediff sql function accept the three arguments start_date, end_date and date_part.
  •  date_part is a part of the date like year months and week that's compare between the start_date and end_date.
  • start_date and end_date are two dates which is going to be compare.They contain the value in the form of  type DATE, DATETIME, DATETIMEOFFSET, DATETIME2, SMALLATETIME, or TIME.

Table for date_part for datediff sql function

sql server datediff
sql server datediff

Return value of sql server datediff function

SQL DATEDIFF() function will return the integer value to indicate the  difference between the start_date and end_date and specified by the date_part.

sql server datediff function return the error if range of the integer return value is (-2,147,483,648 to +2,147,483,647).

 Example for  datediff SQL function  for SQL Server

  •  let see the differences between two date value 

Now use the DATEDIFF() function in SQL to compare two dates dates in various date parts:

DECLARE 
    @start_dt DATETIME2= '2019-12-31 23:59:59.9999999', 
    @end_dt DATETIME2= '2020-01-01 00:00:00.0000000';

SELECT 
    DATEDIFF(year, @start_dt, @end_dt) diff_in_year, 
    DATEDIFF(quarter, @start_dt, @end_dt) diff_in_quarter, 
    DATEDIFF(month, @start_dt, @end_dt) diff_in_month, 
    DATEDIFF(dayofyear, @start_dt, @end_dt) diff_in_dayofyear, 
    DATEDIFF(day, @start_dt, @end_dt) diff_in_day, 
    DATEDIFF(week, @start_dt, @end_dt) diff_in_week, 
    DATEDIFF(hour, @start_dt, @end_dt) diff_in_hour, 
    DATEDIFF(minute, @start_dt, @end_dt) diff_in_minute, 
    DATEDIFF(second, @start_dt, @end_dt) diff_in_second, 
    DATEDIFF(millisecond, @start_dt, @end_dt) diff_in_millisecond;

OUTPUT

DATEDIFF() function in SQL
DATEDIFF() function in SQL

Sunday, November 29, 2020

, ,

meshgrid MATLAB Function With It's example surface plot of a function.

meshgrid matlab Function:-

Helps to generate X and Y matrices for three-dimensional plots.

Syntax For meshgrid: -

[X,Y] = meshgrid(x,y)
[X,Y] = meshgrid(x)
[X,Y,Z] = meshgrid(x,y,z)

Description for meshgrid matlab Function

  • meshgrid function will help to trans transforms the given domain which is specified by the  x and y into arrays X and Y and these array can be used to check the two variables and three-dimensional mesh/surface plots.
  •  Output rows of array X will be copies of x vector; same as the columns  output array Y will be copies of y vector
  •  [X,Y] = meshgrid(x) is like [X,Y] = meshgrid(x,x).
  •  [X,Y,Z] = meshgrid(x,y,z) helps to produces the three-dimensional arrays which is used to check the desired functions of three variables and three-dimensional volumetric plots.

 Important Note for meshgrid matlab Function

  •  The meshgrid function in MATLAB is same as the ndgrid function but the point that should be remembered that is "in meshgrid function the order of  first two input and output arguments is switched"

     [X,Y,Z] = meshgrid(x,y,z)

gives the same result as

    [Y,X,Z] = ndgrid(y,x,z)

  •   meshgrid function in MATLAB is surely suited for two- or three-dimensional Cartesian space.
  •  ndgrid fucntion in MATLAB is suited for multidimensional problems that are not spatially based.

 Example for meshgrid matlab Function

    [X,Y] = meshgrid(1:3,10:14)

    X =

         1     2     3
         1     2     3
         1     2     3
         1     2     3
         1     2     3

    Y =

        10    10    10
        11    11    11
        12    12    12
        13    13    13
        14    14    14

use meshgrid to create a surface plot of a function.

    [X,Y] = meshgrid(-2:.2:2, -2:.2:2);                                
    Z = X .* exp(-X.^2 - Y.^2);                                        
    surf(X,Y,Z)


meshgrid to create a surface plot of a function.
meshgrid to create a surface plot of a function.

Friday, November 27, 2020

,

ord()Function in python with it's example

 ord function in python is used to return an integer representing of Unicode character.

Syntax:-

ord(ch)

Parameters For ord function in python

This function take just only one perameter and that known as ch (a Unicode character)

Return value

The ord() function in python returns an integer representing the Unicode character.

Example for ord()Function in python

print(ord('5'))    # 53
print(ord('A'))    # 65
print(ord('$'))    # 36

OUTPUT

ord()Function
ord()Function in python

Wednesday, November 25, 2020

, ,

hasattr() function in python with example

 Python hasattr() function in python return the true if the object gives the name of attribute and false if didn't return the name.

Syntax for hasattr()

hasattr(object, name)

hasattr() in python called by the getattr() to check about the error


Parameters For hasattr() function in python

There are two parameters which is used for this function the first one is object whose name is going to be checked.

And the name is second parameter which give the name for searched

Return value

It gives boolean return value True and False

Program Example For hasattr() in Python

class Person:
    age = 22
    name = 'Adam stiffman'

person = Person()

print('Person has age?:', hasattr(person, 'age'))
print('Person has salary?:', hasattr(person, 'salary'))

Output

Person has age?: True
Person has salary?: False

Tuesday, November 24, 2020

, , ,

Use of Range Function In Python With Program

 python range function in python return the sequence of numbers between the start integer value to the stop integer.

Syntax : -

range(stop)
range(start, stop[, step])

Parameters For range() function in python:-

Range function takes the three arguments which is given below :-

  •  first one is a start point which define the starting point of range function.
  • second one is stop point integer which help to tell the end point for the sequence.
  • Step is a third one and it use to define the increment for the sequence of number.

Return value from range function in python: -

  •  range() function return the immutable sequence of number.
  • In this function sequence of number starts form 0 to Stop-1.
  • if the step argument is then it raise an Value Error.

How to use the range function in python with program

# empty range
print(list(range(0)))

# using range(stop)
print(list(range(10)))

# using range(start, stop)
print(list(range(1, 10)))

Output:

range function program in python
range function program in python


Monday, November 23, 2020

, ,

fmincon Matlab Function With fmincon Example

fmincon function in MATLAB is used to find the minimum of any given nonlinear multivariable function.

MATLAB Syntax : - 

 

MATLAB Syntax
MATLAB Syntax

Description For fmincon MATLAB

 Nonlinear programming solver.

Let see the a problem here and find the minimum value for this problem.

fmincon MATLAB
fmincon MATLAB

 Well b and beq are vectors, A and Aeq are matrices in this given equation on the other hand c(x) and ceq(x) are the functions in equation that return vectors.

well as you can see in the equation f(x) is a function which returns a scalar and remember all the three function f(x),ceq(x), and c(x) are non linear.

lb,x and ub can be passed as vectors or matrices in the fmincon MATLAB.

  •  x = fmincon(fun,xθ,A,b) function is started with the xθ (x theta) and try to find the minimizer x for the given function and the expression for this  A*x ≤ b. xθ can be a scalar, vector, or matrix.
  •  x = fmincon(fun,x0,A,b,Aeq,beq) minimize this function to linear equalities Aeq*x = beq and A*x ≤ b.If there is no inequalities exist, then set A = [] and b = [].
  • x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub) helps to define the lower and the upper case on the design variables in x, that's why the solution always exixt in between lb ≤ x ≤ ub.
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon) will helps to minimization to the nonlinear inequalities c(x) or the equalities ceq(x) defined in nonlcon.
 
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options) function helps to minimizes with the optimization options which is specified in options.

x = fmincon(problem) helps to finds the minimum for the given problem, and a structure described in problem.
 
[x,fval,exitflag,output] = fmincon(___) will helps to return a value exitflag that describes the exit condition of fmincon function MATLAB.


Example for fmincon function MATLAB

Find the minimum value of Rosenbrock's function if there is a linear inequality constraint. 

 

 

Example for fmincon
Example for fmincon function MATLAB


 

Saturday, November 21, 2020

, , ,

sprintf Function In MATLAB With Example Program

 stg = sprintf(formatSpec,B1,...,Bn) use to formats the data in an array with the help of  formatting operators which is specified by formatSpec in MATLAB and returns the resulting text in a define variable(stg) then stg known as output and otherwise stg is an character.

MATLAB Program for Literal Text and Array Inputs

formatSpec = 'The array is %dx%d.';
A1 = 2;
A2 = 3;
str = sprintf(formatSpec,A1,A2)

Sprintf Program example for MATLAB
Program example for MATLAB

 

compose function is used to return the number of pieces of formatted text as a string array and that can be used with the MATLAB sprintf() Function.

str = sprintf(literalText) In MATLAB use to translates escape-character sequences of the literalText which is define with sprintf function in MATLAB for example :-  \n and \t and sprint function return the all other  characters unaltered.

Now if the string contain any  formatting operator (example %f) then sprint function in MATLAB discards it and all characters after that operator. 

Input Arguments for sprintf function in MATLAB

  • The Output is format with the help of  formatSpec and this also include the ordinary text and special characters.
  • if  formatSpec is include the escape characters in string then  sprintf Function translates that escape characters (such as /n).

Formatting Operator of sprintf

A formatting operator for sprintf starts with a percent sign, %, and ends with a conversion character and the conversion character is also required.

You can also specified some identifier like subtype operators, field width, precision, and  flags between % and  conversion character which is an end point.

Read more  : -  fmincon Matlab Function

List Of Conversion Character for Sprintf function in MATLAB

There some of the Conversion Character

Conversion Character for Sprintf function in MATLAB
Conversion Character for Sprintf function


Some tips : - 

    The sprintf function in MATLAB is similar to fprintf function but the fprintf function is used for the Command Window.