Simplest way to calculate first and last day of month and week for specific date in MS Sql Server

I have come up with following few lines that illustrate a way to find first and last day(date) for specified date.

And then given same thing for week.

Declare @SpecifiedDate DateTime;
Set @SpecifiedDate=GETDATE();
Declare @XStart int;
Set @XStart=0;

SELECT @XStart=DATEDIFF(mm,0,@SpecifiedDate)
SELECT DATEADD(mm,@XStart,0) ‘First Day of Specific Month’
SELECT DATEADD(d,-1,DATEADD(mm,@XStart+1,0)) ‘Last Day of Specific Month’

SELECT DATEADD(wk,DATEDIFF(wk,0,@SpecifiedDate ),0) ‘First Day of Week’
SELECT DATEADD(wk,DATEDIFF(wk,0,@SpecifiedDate ),6) ‘Last Day of Week’

Sticky Div, Area that remains at top on scroll of page

If you want your site header or  menu or other section(div) to remain at top of page when user scrolls a page. You can try with following jquery code.

It will not workin in IE. So first flag need check if browser is IE or not.

if (!IsIE) {
jQuery(document).ready(function() {
jQuery(window).scroll(SomeScrollResponder);
});
function SomeScrollResponder() {
var tmpScroll = $(window).scrollTop();

//when page scrolls 100 px then following  code will run
if (parseInt(tmpScroll) > 100) {
jQuery(“#divTop”).css(“position”, “absolute”);
jQuery(“#divTop”).css(“top”, (parseInt(tmpScroll) – 1) + “px”);  //-1 will hide top border
jQuery(“#divTop”).css(“width”, (parseInt($(document).width()) – 60) + “px”);

}
else {
jQuery(“#divTop”).css(“position”, “relative”);
jQuery(“#divTop”).css(“top”, “0px”);
}
}
}

In above example “divTop” is a div that will display at top, overlapping on page.

 

Embed Gujarati font in website / Embed any other font in web page

Use following definition, which is pointing to a font file located on web server, and a class that has ‘font-family’ attribute with your custom defined font.

@font-face
{
font-family: “myguj-font”;
src: url(“Gujrati-Saral-1.ttf”) format(“truetype”);
font-weight: normal;
font-style: normal;
}
.myguj
{
font-family: “myguj-font”;
}

And your display area need just to point to a style class as follow:

<div class='myguj'>
This text will appear in Gujarati Font.
</div>

Find and download TTF, EOT, SVG, WOFF kind of font files and save in web folder, then you can refer via style sheet(css) file and embed any kind of font in your web page.

My personal website Siddharth Borania’s Personal Website

Output parameter in stored procedure with ExecuteReader method

Conventionally ExecuteReader method is used for returning DataReader object.

Which in turn you can use to fill DataTable.

What if you want a DataTable and some return argument from your stored procedure?

One way is explained below.

Your stored procedure will have one OUTPUT parameter , that will give you number returned form database:

CREATE PROCEDURE GETPRODICTS

@Keyword nvarchar(20),

@CNT int OUTPUT

AS

Select * from product where productname like ‘%’ + @Keyword  + ‘%’

Select @CNT=count(*) from product where productname like ‘%’ + @Keyword  + ‘%’

GO

And in ASP.Net Code should be like:

Int32 intCNT;

DataTable xTbl = new DataTable();

using (SqlConnection conn = new SqlConnection(connectionString))  

 {

SqlCommand cmd= new SqlCommand();

cmd.Connection = conn;

cmd.CommandType= CommandType.StoredProcedure;

cmd.CommandText = “GETPRODUCTS”;

 

SqlParameter[]  tmpParam = new SqlParameter[] {

                new SqlParameter(“@keyword”, SqlDbType.NVarChar,20),

                new SqlParameter(“@CNT”, SqlDbType.Int)};

 

                tmpParam [0].Value =”test”;

                tmpParam [1].Direction = ParameterDirection.Output;

 

cmd.Parameters.Add(tmpParam);

SqlDataReader rdr =cmd.ExecuteReader(CommandBehavior.CloseConnection);

                if (rdr.HasRows)

                {  xTbl.Load(rdr);  }

                rdr.Close();

                intCNT = int.Parse(cmd.Parameters[1].Value.ToString());

    }

Make sure you CLOSE data reader object before retrieving values from parameter.

So here you will get DataTable with records from data base  and Integer variable (intCNT)  with record count from same table.

 

Other Technical Stuff from Siddharth:

Show current date using Javascript in web browser(Client side date in web browser
https://siddharthboraniait.wordpress.com/2013/09/11/show-current-date-using-javascript-in-web-browserclient-side-date-in-web-browser

 

How to make custom Task Scheduler in asp.net c# using thread
https://siddharthboraniait.wordpress.com/2013/09/06/howto-make-custom-task-scheduler-in-asp-net-csharp-using-thread

 

Introduction to Andriod – First step towards Andriod application development
https://siddharthboraniait.wordpress.com/2011/07/18/know-andriod-introduction-to-andriod

Use ROW_NUMBER in SQL to retrive Paging specific records from table.

In SQL Server,

ROW_NUMBER() function returns number of row from result set(you have requested thru query).

 

Following sample query will result in only 10 records from Product table.

WITH DummyTable AS (
SELECT
ROW_NUMBER() OVER(order by ProductName ASC ) as RowNum,
ProductId,ProductName
from ProductMaster
)
select * from DummyTable
where rownum>=210 AND rownum<220

 

Such query is useful if you want to implement paging in your web application/project and  you want to retrieve only records from table according to your current page.

Display Image in html as Binary Data with Base64 encoding

In HTML, traditionally we use path of image file to specify image source in IMG tag.

However, we can give binary data as well as source in “SRC” attribute of IMG tag, and image will be display in html.

Syntax:

<IMG SRC=”data:image/gif;base64,<Binary Data>” />

Example:

<IMG SRC=”data:image/gif;base64, R0lGODlhCAAFAIABAMaAgP///yH5BAEAAAEALAAAAAAIAAUAAAIKBBKGebzqoJKtAAA7″ />

Binary data in above line is ‘base64 encoded’ format of image.

One advantage of this method is that visitor never knows the PATH of image you have displayed.

Drawback of this point is that IE does not support this way to display image.

 

Other Technical Stuff from Siddharth:

Show current date using Javascript in web browser(Client side date in web browser
https://siddharthboraniait.wordpress.com/2013/09/11/show-current-date-using-javascript-in-web-browserclient-side-date-in-web-browser

 

How to make custom Task Scheduler in asp.net c# using thread
https://siddharthboraniait.wordpress.com/2013/09/06/howto-make-custom-task-scheduler-in-asp-net-csharp-using-thread

 

Introduction to Andriod – First step towards Andriod application development
https://siddharthboraniait.wordpress.com/2011/07/18/know-andriod-introduction-to-andriod