Skip to main content


Showing posts from August, 2016

Query to subtract in MongoDB

We can use $subtract operator to subtract tow numbers. $subtract is arithmetic aggregation operator which subtract two number and returns the difference.
$subtract can be used to get differences between dates means we can subtract date also, $subtract treat to the second number as millisecond if the first number is date type.

{ $subtract: [ <expression1>, <expression2> ] }

Example: Suppose we have a collection “Inventory” as below and before showing the details of the product on our website we need to calculate the total price in our query.

{             _id:1,             Title:"Title1",             SKU:"1234567890123",             Price:20.50,             ShippingCharge:4.80,             Discount:2.00,             CreatedDate:ISODate("2016-08-09T013:00:00Z") } {             _id:2,             Title:"Title2",             SKU:"1234567890121",             Price:30.50,             ShippingCharge:4.80,             Discount:2.00,          …

How to get absolute value in MongoDB

To get the absolute value we can use $abs operator. $abs is the arithmetic aggregate operator which returns the absolute value of the number. Absolute value means it always returns positive value while the number may positive or negative.
Before version of MongoDB 3.2, we can’t use directly on the number like below.
{ $project: { number: { $abs: '$number } }  
We needed some calculation as below.
db.Col.aggregate([    {      $project: { abs_value: { $abs: { $subtract: [ 1, 2 ] } } }    }]). Syntax:

{ $abs: <number> }

Example: Run the following script which $subtract will return negative value but $abs will return the absolute value of subtract.

db.Col.aggregate([    {      $project: { abs_value: { $abs: { $subtract: [ 1, 2 ] } } }    } ])


db.ratings.aggregate([    {      $project: { abs_value: { $abs: -1 } }    } ])

It will return abs_value =1.0
Note: If the argument field is missing or null it will return null.

Shorthand way to return values that might be null

Problem: Suppose we have get-only property as below. In this we have a private variable and a public property both are list type.

privateList<User> _usr ; publicList<User> lst { get { if (_usr == null) { _usr = newList<User>(); }
return _usr; } }

How can I optimize this code or any shorthand way to write code of above scenario?

How to get name, data type and fields size of table in SQL Server

Sometimes we need to see the column details like name, data type and size, this article may help you to find the details of column is database table.
Suppose we have an”Inventory” table as below.

CREATETABLE[dbo].[Inventory2]( [ID][bigint]IDENTITY(1,1)PRIMARYKEYNOTNULL, [Title][varchar](500)NULL, [SKU][varchar](20)NULL, [Price][decimal](18, 2)NULL, [Created_Date][datetime]NULL, [Updated_Date][datetime]NULL, ) GO

And we need to see the details of above table’s columns, run the following script it may help to find the details of table’s columns like Name, Data_Type, and Length.

SELECTcolumn_nameas'COL_NAME',data_typeas'Data_Type', Col_Length(table_name,column_name)as'MAX_LENGTH' FROMinformation_schema.columns WHERE

Difference between function and stored procedure in SQL Server

Some basic difference between stored procedure and function is described below.
The main difference between stored procedure and function is Function must returns a value while stored procedure can or not this is optional. Function can have input parameter only while stored procedure can have both input/output parameters. Function can be called from stored procedures but stored procedures can’t call from function.
Stored Procedure Function must returns a value
In stored procedure it is optional Function can have input parameter only Stored procedure can have both input and output parameter s Function can be called from procedure
Procedure can‘t be called from function Function allows only select statement Procedure allows DML statement(SELECT, INSERT, UPDATE, DELETE) Function can use in SQL statement like WHERE/HAVING/SELECT

Paging in SQL server 2012 stored procedure using OFFSET and FETCH

In this article we will learn about pagination using OFFSET and FETCH, OFFSET and FETCH is the new features of SQL Server 2012. In earlier version this feature is not available so was very difficult to implement paging in our procedures it also kill procedures performance.
Pagination is very easy in SQL Server 2012. New feature of SQL Server 2012(OFFSET and FETCH) is described below.
OFFSET: OFFSET skips the specific number of rows. The number can be zero or greater than zero.
FETCH: FETCH returns specific number of rows after processing the OFFSET clause. The number can be one or greater than one.
OFSET Syntax:

OFFSET { integer_constant | offset_row_count_expression } { ROW | ROWS }

FETCH Syntax:

FETCH { FIRST|NEXT } <rowcount expression> { ROW|ROWS } ONLY

Limitation of OFFSET and FETCH: If you are using OFFSET and FETCH, it’s mandatory to use ORDER BY clause.OFFSET is mandatory if you are using FETCH.In query expression you can’t use TOP with OFFSET and FETCH.You can’t use COUNT(*…