Monday, December 22, 2014

Get End Of Month in SQL Query

There is in built function of SQL is available to get the End of the Month Date.

EOMONTH(StartDate, MonthToAdd)



Examples:

SELECT EOMONTH ( GETDATE() ) AS 'This Month';
SELECT EOMONTH ( GETDATE(), 1 ) AS 'Next Month';
SELECT EOMONTH ( GETDATE(), -1 ) AS 'Last Month';

How to Get the logged in SQL User Name

Hi Dear All,

There is small in build function of SQL to get the logged in SQL User,

              SUSER_SNAME ( [ server_user_sid ] ) 
server_user_sid (Security Identification Number) is optional parameter for the function, it can be SQL Server Login or Windows User.
If this parameter is not passed it will result the currently logged in user name.

Use:
SUSER_SNAME can be used as DEFAULT constraint for the column values.



Sunday, November 09, 2014

Best Practice to write SQL - Stored Procedure.

Hi Friends,

As every application has a Database, and some objects which are helping us to perform the actions on our Data, here I am going to tell Stored Procedure SQL Object related best practices.

  • Use the SET NOCOUNT ON statement as the first statement in the body of the procedure. That is, place it just after the AS keyword.
  • Use schema names when creating or referencing database objects in the procedure. It will take less processing time for the Database Engine to resolve object names if it does not have to search multiple schemas.
  • Avoid wrapping functions around columns specified in the WHERE and JOIN clauses. Doing so makes the columns non-deterministic and prevents the query processor from using indexes.
  • Avoid using scalar functions in SELECT statements that return many rows of data. Because the scalar function must be applied to every row, the resulting behaviour is like row-based processing and degrades performance. 
  • Avoid the use of SELECT *. Instead, specify the required column names.
  • Use explicit transactions by using BEGIN/END TRANSACTION and keep transactions as short as possible. Longer transactions mean longer record locking and a greater potential for deadlocking.
  • Use the Transact- SQL TRY…CATCH feature for error handling inside a procedure.
  • Use the DEFAULT keyword on all table columns that are referenced by CREATE TABLE or ALTER TABLE Transact-SQL statements in the body of the procedure. This will prevent passing NULL to columns that do not allow null values.
  • Use NULL or NOT NULL for each column in a temporary table.
  • Use the UNION ALL operator instead of the UNION or OR operators, unless there is a specific need for distinct values. The UNION ALL operator requires less processing overhead because duplicates are not filtered out of the result set.


hope it will help to increase your application performance.


Write exceptions to System Event Log.

Hi Dear friends,

I was in need to use the Operating System Event Logs, to save the c# exceptions, where I got the following information from our coder's guru... (Google)

Namespace to be included : System.Diagnostics
Classes: All classes in above mentioned namespace allow us to communicate with the System Processes, Event Logs and Performance Counters.

Following is the code snippet where I have used EventLog class default constructor to log the event to the system event viewer

Output to the above Code snippet

Thanks,
Hope it helps.

Tuesday, July 08, 2014

Find column exists in SQL database (COL_LENGTH)

COL_LENGTH

It is a built in function, which returns the length in bytes of the column.

COL_LENGTH('Table_Name','Column_Name')

Table_Name: Name of the table for which column length info required, it is nvarchar type expression.
Column_Name: Name of the column who's length is required, it is also nvarchar type expression.

It returns NULL on error or if users do not have permission to view object.

Wednesday, July 02, 2014

Bramhakamal (Orchid Cactus)

Hi Dear all,

Near to my home (in Bangalore) I have captured the growth of Bramhakamal flower growth, please have a look, snaps are here with little information for you all....
Early Stage - Bud
It belongs to Cactacease. Also known as Night Blooming Cereus, Queen of the Night or Lady of the Night, it is beautiful lotus like flower.


For planting, you can cut the stem / leaf and plant in the soil which has acidic features, or you can put the stem / leaf in water for around 3 weeks, after 3 week it will produce roots and then you can plant it in soil.











This flower bloom only for a night, blooming will start around 1900 hours and after 2 hours it blooms fully, early in the morning it drops.










Flower




Nice use by Mother for worshiping