Javascript: Thousand separator


We may use following javascript function to show numbers formatted with thousands separator.

Definition

function formatThousandPlace(orgNum) {
    arr = orgNum.split('.');    //split integer part & fractional part
    intPart = arr[0];           //store integer part in a variable
    fractionPart = arr.length > 1 ? '.' + arr[1] : '';  //store fractional part in a variable
    var rgx = /(\d+)(\d{3})/;   //regexp to find thousand place
            
    while (rgx.test(intPart)) { 
        intPart = intPart.replace(rgx, '$1' + ',' + '$2');  //replace integer part after inserting a comma in the thousandth place
    }
    
    document.write(intPart + fractionPart); //write final value to the document
}

Function Call
Now, we can use it anywhere in the document where we want to show those numbers. For example: it may inside normal html elements or some dynamic ASP.Net controls like GridView, DataList, FormView etc.

<script language="javascript">formatThousandPlace('10000000.00')</script>

While calling this from ItemTemplate of an ASP GridView the syntax will be

<asp:TemplateField HeaderText="Price">
    <ItemTemplate>
       <script language="javascript">formatThousandPlace('<%#Eval("SalePrice") %>')</script>
    </ItemTemplate>
</asp:TemplateField>

I hope this is helpful.

Advertisements

Javascript: Function to get current fiscal year


This can be useful when you need to print current session year or, current fiscal/financial year somewhere in your page and specifically when you do not want to invoke a server method for it.

Defining Function


<script language="javascript">
 function getCurrentFiscalYear() {
    //get current date
    var today = new Date();
    
    //get current month
    var curMonth = today.getMonth();
    
    var fiscalYr = "";
    if (curMonth > 3) { //
        var nextYr1 = (today.getFullYear() + 1).toString();
        fiscalYr = today.getFullYear().toString() + "-" + nextYr1.charAt(2) + nextYr1.charAt(3);
    } else {
        var nextYr2 = today.getFullYear().toString();
        fiscalYr = (today.getFullYear() - 1).toString() + "-" + nextYr2.charAt(2) + nextYr2.charAt(3);
    }
    
    document.write(fiscalYr);
 }
 </script>

Calling Function

<script language="javascript">getCurSession()</script>

Note:

  • I have made the function as per Indian fiscal year. You can change it according to your country. You only need to modify the conditional statement i.e, if (curMonth > 3) where April(3) is the starting month of fiscal year.
  • getMonth() returns number of the month in a date value starting from 0 to 11
  • You can change this function to return the session year which can be input for another logic/function. Accordingly you have to change the function.

SQL Server: encrypt password


This post is about encrypting a value before inserting it to your password  field in SQL Server. This is very much similar as PASSWORD(),SHA1(), etc. in MySQL. In SQL Server we can use the same through PWDENCRYPT() function.

Syntax for insert:


INSERT INTO Table1 (field1,field2) VALUES(value1, PWDENCRYPT(value2))

Example:


INSERT INTO dbo.UserLogin (Uid,Pwd) VALUES('User', PWDENCRYPT('Suvendu'))

Here, ‘Suvendu’ is the value for my Pwd field which I want to encrypt.

Syntax for comparing:


SELECT * FROM Table1 WHERE field1=value1 AND field2=PWDENCRYPT(value2)

Example:


SELECT * FROM dbo.UserLogin WHERE Uid='User' AND Pwd=PWDENCRYPT('Suvendu')

Reverse  is not possible, I mean decryption can not done for this encrypted value.

NOTE: It is a undocumented function. As far I know, PWDENCRYPT() is supported by SQL Server version upto SQL Server 2012 (beta) but, MSDN says it may not be available there in the future versions of SQL Server.

SQL Server: Function to get previous session year


When you need to get previous session year of a supplied session year then you can use the below function.

Step-1 :

Execute below script.


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Suvendu>
-- Create date: <13 Mar 2012>
-- Description: <returns prev sess yr of the supplied sess yr>
-- =============================================
CREATE FUNCTION [dbo].[fuGetPrevSessionYr]
(
 @SessYr NVARCHAR(7)
)
RETURNS NVARCHAR(7)
AS
BEGIN
 DECLARE @PrevSessYr NVARCHAR(7)
 SELECT @PrevSessYr=CAST((CAST(LEFT(@SessYr,4) AS INT)-1) AS VARCHAR(4))+'-'+RIGHT(LEFT(@SessYr,4),2)

 RETURN @PrevSessYr
END

Step-2:

Syntax for calling the function-

SELECT dbo.fuGetPrevSessionYr(<SessionYr>) [FROM TABLE1]

Where  <SessionYr> is the session year, whose previous session you want to show.

SQL Server: Function to get fiscal year or, session year


You can use this function to get the fiscal/financial year from a supplied date value. This may be useful when you have a datetime column in a table but you want to show it’s fiscal year. Another case when you may need this function is you have a datetime column in your table but you have to show the transaction made, specified fiscal year/session year wise or when you are preparing any statistical report.


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Suvendu>
-- Create date: <10 mar 2012>
-- Description: <returns the fiscal year of the supplied date>
-- =============================================
CREATE FUNCTION [dbo].[fuGetFiscalYear]
(
 @SuppliedDate DATETIME
)
RETURNS NVARCHAR(7)
AS
BEGIN
 DECLARE @SessYr NVARCHAR(7)
 SELECT @SessYr= CASE WHEN DATEPART(MONTH,@SuppliedDate)>3 THEN CAST(DATEPART(YEAR,@SuppliedDate) AS VARCHAR(4))+'-'+RIGHT(CAST(DATEPART(YEAR,@SuppliedDate)+1 AS VARCHAR(4)),2) ELSE CAST(DATEPART(YEAR,@SuppliedDate)-1 AS VARCHAR(4))+'-'+RIGHT(CAST(DATEPART(YEAR,@SuppliedDate) AS VARCHAR(4)),2) END
 RETURN @SessYr
END
GO

[Note: I have assumed the fiscal year as Indian fiscal year or, financial year. You can change it accordingly or, can use the below function]


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Suvendu>
-- Create date: <10 mar 2012>
-- Description: <returns the fiscal year of the supplied date>
-- =============================================
CREATE FUNCTION [dbo].[fuGetSessionYear]
(
 @SuppliedDate DATETIME,
 @StartMonth INT
)
RETURNS NVARCHAR(7)
AS
BEGIN
 DECLARE @SessYr NVARCHAR(7)
 SELECT @SessYr= CASE WHEN DATEPART(MONTH,@SuppliedDate)>@StartMonth THEN CAST(DATEPART(YEAR,@SuppliedDate) AS VARCHAR(4))+'-'+RIGHT(CAST(DATEPART(YEAR,@SuppliedDate)+1 AS VARCHAR(4)),2) ELSE CAST(DATEPART(YEAR,@SuppliedDate)-1 AS VARCHAR(4))+'-'+RIGHT(CAST(DATEPART(YEAR,@SuppliedDate) AS VARCHAR(4)),2) END
 RETURN @SessYr
END
GO