Tuesday 24 January 2017

Converting Comma Separated Value in to Rows in SQL Server function


// Sql query

CREATE FUNCTION Fun_BreakStringIntoRows (@CommadelimitedString   varchar(1000))
RETURNS   @Result TABLE (Column1   VARCHAR(100))
AS
BEGIN
        DECLARE @IntLocation INT
        WHILE (CHARINDEX(',',    @CommadelimitedString, 0) > 0)
        BEGIN
              SET @IntLocation =   CHARINDEX(',',    @CommadelimitedString, 0)     
              INSERT INTO   @Result (Column1)
              --LTRIM and RTRIM to ensure blank spaces are   removed
              SELECT RTRIM(LTRIM(SUBSTRING(@CommadelimitedString,   0, @IntLocation)))  
              SET @CommadelimitedString = STUFF(@CommadelimitedString,   1, @IntLocation,   '')
        END
        INSERT INTO   @Result (Column1)
        SELECT RTRIM(LTRIM(@CommadelimitedString))--LTRIM and RTRIM to ensure blank spaces are removed
        RETURN
END

GO


// Execute  Sql query

select * from Fun_BreakStringIntoRows('1, 2, 3') where BreakStringIntoRows.Column1=1


//  Result  


Friday 20 January 2017

Get Name of days between tow dates in sql server



//           Create function


Create FUNCTION [dbo].[Get_NameOf_Days](
@startdate datetime
,@enddate datetime
,@DayValue int -- return the value of days (starting day with sunday- value=1)
,@NameValue int -- return the name of days (starting day with monday- value=0)
)
RETURNS VARCHAR(MAX)
BEGIN
declare @count int =0
    while @startdate<=@enddate
    Begin
        IF DatePart(WEEKDAY,@startdate) = @DayValue
            SET @count=@count+1
        SET @startdate=DateAdd(d,1,@startdate)
    END
RETURN  (select DATENAME(dw,@NameValue)+' : '+CAST(@count as varchar))

END


//           Call  function

select dbo.Get_NameOf_Days('2017-1-1','2017-1-31',1,6)



//           Result


Wednesday 18 January 2017

Sql date difference in years months and days


//// Method 1

DECLARE @getmm INT,@yy int ,@mm int,@dd int,@getdd INT,@dstart datetime='2010-01-01',@dend datetime='2016-06-12'

SET @yy = DATEDIFF(yy, @dstart, @dend)
SET @mm = DATEDIFF(mm, @dstart, @dend)
SET @dd = DATEDIFF(dd, @dstart, @dend)
SET @getmm = ABS(DATEDIFF(mm, DATEADD(yy, @yy, @dstart), @dend))
SET @getdd = ABS(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, DATEADD(yy, @yy, @dstart), @dend), DATEADD(yy, @yy, @dstart)), @dend))

select 'Year : '+ Convert(varchar(10),@yy) + ',    ' +'Month : '+ Convert(varchar(10),@getmm) + ',    '  + 'Days : '+Convert(varchar(10),@getdd)

/// Result


Monday 9 January 2017

Set Custom error page for all Application label error and error code also

//  IN Global.asax Under Application_Error

void Application_Error(object sender, EventArgs e)
    {
        //////////// Code that runs when an unhandled error occurs

        //Response.Redirect("~/Error Page Not Found");

        Exception ex = Server.GetLastError();
        Application["TheException"] = ex; //store the error for later
        Server.ClearError(); //clear the error so we can continue onwards
        Response.Redirect("~/Error Page Not Found"); //direct user to error page

    }



// IN web.config file Under

  <system.web>
<!--<customErrors mode="Off"></customErrors>-->
<customErrors mode="RemoteOnly" defaultRedirect="~/Error Page Not Found"></customErrors>

  </system.web>

<system.webServer>

    <httpErrors errorMode="Custom" defaultResponseMode="Redirect">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" prefixLanguageFilePath="" path="~/Error Page Not Found"
       responseMode="Redirect" />
    </httpErrors>

    <validation validateIntegratedModeConfiguration="false"/>

</system.webServer>

Monday 2 January 2017

Responsive Table CSS (No bootstap)

// HTML

<table id="Table1" class="listtablecss col-lg-11 col-md-11 col-sm-11 col-xs-12" border="0"
                cellpadding="0" cellspacing="5">
                <tbody>
                    <tr>
                        <td>
                            <input id="asdfasd_0" name="asdfasd$0" type="checkbox"><label for="asdfasd_0">Sarva
                                siksha abhiyan</label>
                        </td>
                        <td>
                            <input id="asdfasd_1" name="asdfasd$1" type="checkbox"><label for="asdfasd_1">Mid-day-meal</label>
                        </td>
                        <td>
                            <input id="asdfasd_2" name="asdfasd$2" type="checkbox"><label for="asdfasd_2">Pre-matric
                                scholarship scheme</label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input id="asdfasd_3" name="asdfasd$3" type="checkbox"><label for="asdfasd_3">Post-matric
                                scholarship scheme</label>
                        </td>
                        <td>
                            <input id="asdfasd_4" name="asdfasd$4" type="checkbox"><label for="asdfasd_4">Deucation
                                sheme for Minorities</label>
                        </td>
                        <td>
                            <input id="asdfasd_5" name="asdfasd$5" type="checkbox"><label for="asdfasd_5">Other
                                specify</label>
                        </td>
                    </tr>
                </tbody>

            </table>
// CSS
.listtablecss{width: 100%;text-align: left;table-layout: fixed;}       
        .listtablecss th, td
        {word-wrap: break-word; width: 100%;padding: 4px 0px !important;}       
        @media screen and (max-width: 750px)
        {
            .listtablecss tbody, thead{float: left;}
            .listtablecss tbody{min-width: 120px;}
            .listtablecss td, th{display: block;padding: 4px 0px !important;}

        }


//OUTOUT




Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...