Wednesday, February 4, 2009

Javascript - Compare two Dates

Javascript - Compare two Dates

function CompareDates() {
var txtEffDt = document.getElementById("txtEffectiveDt").value.split("/");
var txtExpDt = document.getElementById("txtExpiryDt").value.split("/");
var date1 = new Date(txtEffDt[2]+'/'+txtEffDt[0]+'/'+txtEffDt[1]);
var date2 = new Date(txtExpDt[2]+'/'+txtExpDt[0]+'/'+txtExpDt[1]);

if (date1 > date2) {
alert("Expiry Date cannot be less than Effective Date");
return false;
}
}

Tuesday, February 3, 2009

Javascript - Convert Number to words

Javascript - Convert Number to words

link the script into the head of your page using the following code:



The final step is to call the script to perform the conversion to words for you. To get a number converted to words you just need to call the function passing it the number you want to convert and the corresponding words will be returned.

var words = toWords(num);


// Convert numbers to words
// American Numbering System
var th = ['','thousand','million', 'billion','trillion'];
// uncomment this line for English Number System
// var th = ['','thousand','million', 'milliard','billion'];

var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine']; var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen']; var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety']; function toWords(s){s = s.toString(); s = s.replace(/[\, ]/g,''); if (s != String(parseFloat(s))) return 'not a number'; var x = s.indexOf('.'); if (x == -1) x = s.length; if (x > 15) return 'too big'; var n = s.split(''); var str = ''; var sk = 0; for (var i=0; i < 3="="2)" sk="1;}" sk="1;}}" 3="="0)" sk="1;}" 3="="1)" sk="0;}}" y =" s.length;" i="x+1;">

Convert an Integer/Number into words

SET NOCOUNT ON
=============================================
Description: Converts an integer into words,
viz., --
master.dbo.fnSpellInteger(10129) ==> "Ten Thousand One Hundred Twenty-Nine"
=============================================
USE MASTER
IF OBJECT_ID('dbo.fnSpellInteger') IS NOT NULL DROP FUNCTION fnSpellIntegerGO
CREATE FUNCTION dbo.fnSpellInteger ( @number int )RETURNS VARCHAR(100)ASBEGIN -- For debugging outside of the UDF: DECLARE @debug bit SET @debug = 0
DECLARE @result VARCHAR(100), @word VARCHAR(100), @group VARCHAR(100) DECLARE @i int, @j int, @m int, @digit VARCHAR(2), @cn VARCHAR(20)
IF @number = 0 RETURN 'Zero'
SELECT @result = '', @word = '', @group = '' SET @cn = @number SET @cn = REPLACE(@cn,',','') SET @m = LEN(@cn) % 3 IF @m > 0 SET @cn = REPLICATE('0',3-@m) + @cn -- Left pad with zeroes to a multiple of 3
SET @i = 1 SET @j = LEN(@cn)-@i+1 SET @m = @i % 3 WHILE @i <= LEN(@cn) BEGIN -- @i is 1 origin index into numeric string while @m = @i modulo 3 -- If the middle digit of each group of 3 is a '1' then this is a 'Ten' or a '...teen' IF @m = 2 AND SUBSTRING(@cn,@i,1) = '1' BEGIN SET @digit = SUBSTRING(@cn,@i,2) -- Skip rightmost digit of 3 if processing teens SET @i = @i + 1 END ELSE SET @digit = SUBSTRING(@cn,@i,1)
SET @word = CASE WHEN @m = 0 THEN -- Rightmost digit of group of 3 CASE @digit WHEN '0' THEN '' WHEN '1' THEN 'One' WHEN '2' THEN 'Two' WHEN '3' THEN 'Three' WHEN '4' THEN 'Four' WHEN '5' THEN 'Five' WHEN '6' THEN 'Six' WHEN '7' THEN 'Seven' WHEN '8' THEN 'Eight' WHEN '9' THEN 'Nine' END + CASE WHEN (@group <> '' OR @digit <> '0') AND (@j+2) / 3 = 2 THEN ' Thousand' WHEN (@group <> '' OR @digit <> '0') AND (@j+2) / 3 = 3 THEN ' Million' WHEN (@group <> '' OR @digit <> '0') AND (@j+2) / 3 = 4 THEN ' Billion' ELSE '' END WHEN LEN(@digit) = 2 THEN -- Special case when middle digit is a '1' CASE @digit WHEN '10' THEN 'Ten' WHEN '11' THEN 'Eleven' WHEN '12' THEN 'Twelve' WHEN '13' THEN 'Thirteen' WHEN '14' THEN 'Fourteen' WHEN '15' THEN 'Fifteen' WHEN '16' THEN 'Sixteen' WHEN '17' THEN 'Seventeen' WHEN '18' THEN 'Eighteen' WHEN '19' THEN 'Nineteen' END + CASE WHEN (@group <> '' OR @digit <> '00') AND (@j+2) / 3 = 2 THEN ' Thousand' WHEN (@group <> '' OR @digit <> '00') AND (@j+2) / 3 = 3 THEN ' Million' WHEN (@group <> '' OR @digit <> '00') AND (@j+2) / 3 = 4 THEN ' Billion' ELSE '' END WHEN @m = 2 THEN -- Middle digit of group of 3 CASE @digit WHEN '2' THEN 'Twenty' WHEN '3' THEN 'Thirty' WHEN '4' THEN 'Forty' WHEN '5' THEN 'Fifty' WHEN '6' THEN 'Sixty' WHEN '7' THEN 'Seventy' WHEN '8' THEN 'Eighty' WHEN '9' THEN 'Ninety' ELSE '' END WHEN @m = 1 THEN -- Leftmost digit of group of 3 CASE @digit WHEN '0' THEN '' WHEN '1' THEN 'One' WHEN '2' THEN 'Two' WHEN '3' THEN 'Three' WHEN '4' THEN 'Four' WHEN '5' THEN 'Five' WHEN '6' THEN 'Six' WHEN '7' THEN 'Seven' WHEN '8' THEN 'Eight' WHEN '9' THEN 'Nine' END + CASE WHEN @digit <> '0' THEN ' Hundred' ELSE '' END END
SET @group = @group + RTRIM(@word) -- Group value
IF @word <> '' BEGIN DECLARE @prefix VARCHAR(20) IF CHARINDEX(' ',@word) > 0 SET @prefix = LEFT(@word,CHARINDEX(' ',@word)) ELSE SET @prefix = @word IF RIGHT(@result,2) = 'ty' AND @prefix IN ('One','Two','Three','Four','Five','Six','Seven','Eight','Nine') SET @result = @result + '-' + LTRIM(@word) ELSE SET @result = @result + ' ' + LTRIM(@word) END -- The following needs to be outside of a UDF to work: --IF @debug = 1 SELECT @cn as 'Number', @i as '@i', @j as '@j', @m as '@m', @digit as '@digit', CAST(replace(@group,' ','`') AS CHAR(30)) as '@group', @word as '@word', @result as '@result' SET @i = @i + 1 SET @j = LEN(@cn)-@i+1 SET @m = @i % 3 IF @m = 1 SET @group = '' -- Clear group value when starting a new one
END
IF @result = '' SET @result = '0' RETURN LTRIM(@Result)
ENDGO
SET NOCOUNT ON

Description: Converts a string numeric expression into words,
viz., --
master.dbo.fnSpellNumber('15.99') ==> "Fifteen and Ninety-Nine Hundredths"-- Notes: Uses fnSpellInteger to convert an integer into words-- Example: fnSpellNumber can be used to generate pseudo-random test character data/* set nocount on declare @rand int, @i int set @rand = rand(131)*100 set @i = 0
while @i < 5 begin set @i = @i + 1 select 'insert(id,number,words) values(' + cast(@i as varchar(5)) + ',' + cast(@rand as varchar(5)) + ',''' + master.dbo.fnspellnumber(@rand) + ''')' set @rand = rand()*100 end*/-- =============================================
USE MASTER
IF OBJECT_ID('master.dbo.fnSpellNumber') IS NOT NULL DROP FUNCTION fnSpellNumberGO
CREATE FUNCTION dbo.fnSpellNumber ( @number varchar(20) )RETURNS VARCHAR(200)AS--For debugging: declare @number varchar(20) set @number = '192.1'BEGIN -- This is for use outside of a function: DECLARE @debug bit SET @debug = 0
DECLARE @result varchar(200), @word varchar(100) DECLARE @i int, @intpart varchar(20), @decpart varchar(20) SET @word = LTRIM(RTRIM(@number)) -- Check for a bad number, e.g., one with embedded spaces IF ISNUMERIC(@word) = 0 RETURN '<<>>'
SET @i = CHARINDEX('.', @word) -- Remove trailing zeroes for any decimal portion IF @i > 0 -- Number contains a decimal point BEGIN WHILE RIGHT(@word,1) = '0' SET @word = LEFT(@word,LEN(@word)-1) IF @word = '' SET @word = '0' END -- Insert a decimal point at the end if none was specified IF @i = 0 -- No decimal point BEGIN SET @word = @number + '.' SET @i = CHARINDEX('.', @word) END
SET @intpart = LEFT(@word,@i-1) -- Extract the integer part of the number if any IF LEN(@intpart) > 0 SET @result = master.dbo.fnSpellInteger(CAST(@intpart AS int)) ELSE SET @result = ''
-- Extract the decimal portion of the number SET @decpart = RIGHT(@word,LEN(@word)-@i) -- @i is position of decimal point
IF LEN(@decpart) > 0 BEGIN IF @result = 'Zero' SET @result = '' ELSE IF @result <> '' SET @result = @result + ' and '
SET @result = @result + master.dbo.fnSpellInteger(@decpart) + CASE LEN(@decpart) WHEN 0 THEN '' WHEN 1 THEN ' Tenths' WHEN 2 THEN ' Hundredths' WHEN 3 THEN ' One-Thousandths' WHEN 4 THEN ' Ten-Thousandths' WHEN 5 THEN ' One-Hundred-Thousandths' WHEN 6 THEN ' One-Millionths' WHEN 7 THEN ' Ten-Millionths' WHEN 8 THEN ' One-Hundred-Millionths' WHEN 9 THEN ' One-Billionths' END -- Check for a valid plural IF @decpart = 1 SET @result = LEFT(@result, LEN(@result)-1) -- Remove last "s" for just 1 END -- This is for use outside of a function: if @debug = 1 select @word as '@word', @i as '@i', @intpart as '@intpart', @decpart as '@decpart', @result as '@result' RETURN @result
ENDGO

Tuesday, January 27, 2009

How to increase the size of a database (Enterprise Manager)

How to increase the size of a database (Enterprise Manager)

To increase the size of a database

Expand a server group, and then expand a server.

Expand Databases, right-click the database to increase, and then click Properties.

To increase the data space, click the General tab. To increase the transaction log space, click the Transaction Log tab.

To add a new file, click the next empty row and, in the File name column, enter the file name that will contain the additional space.
The file location is generated automatically and given the .ndf suffix for a database file, or an .ldf suffix for a transaction log file.

To change the default values provided in the File name, Location, Space allocated (MB), and Filegroup (not applicable for the transaction log) columns, click the cell to change and enter the new value.
For existing files, only the Space allocated (MB) value can be changed; the new value must be larger than the existing value.

To specify how the file should grow, select from these options:
To allow the currently selected file to grow as more data space is needed, select Automatically grow file.

To specify that the file should grow by fixed increments, select In megabytes and specify a value.

To specify that the file should grow by a percentage of the current file size, select By percent and specify a value.
To specify the file size limit, select from these options:
To allow the file to grow as much as necessary, select Unrestricted filegrowth.

To specify the maximum size to which the file should be allowed to grow, select Restrict filegrowth (MB) and specify a value.
Note  The maximum database size is determined by the amount of disk space available and the licensing limits determined by the version of SQL Server you are using.

How to Get Available space on Database

How to Get Available space on Database using a Dynamic Query:-

EXEC(N'USE ' + @dbname + N' select  [FILE_SIZE_MB] =
           convert(decimal(12,2),round(size/128.000,2)),
     [SPACE_USED_MB] =
           convert(decimal(12,2),round(fileproperty(name,''SpaceUsed'')/
128.000,2)),
     [FREE_SPACE_MB] =
           convert(decimal(12,2),round((size-
fileproperty(name,''SpaceUsed''))/128.000,2)),
     [DEVICE_NAME] = left(NAME,15)
from  dbo.sysfiles;')

Friday, January 16, 2009

Minus Keyword in SQL Server 2000

Minus Keyword in SQL Server 2000
Oracle supports "Minus" Keyword, but today suddenly when i tried to execute same in SQL Server it won't work. So, here is an alternate way to use "Minus" in SQL Server:-
Example:
Select City, Statefrom Employee Minus Select City, State from Customer
This will work fine with Oracle, but won't work with SQL Server. So alternate way to use "Minus" Keyword in SQL Server:-
Select City, Statefrom Employee Except Select City, State from Customer

Tuesday, January 13, 2009

Create A Comma Delimited List From a Column

Create A Comma Delimited List From a Column
Create Sample Data for Both Solutions
-- Suppress data loading messages SET NOCOUNT ON   -- Create Sample Data  CREATE TABLE TeamInfo    (  MemberID    int IDENTITY,       TeamID      int,        FirstName   varchar(50)    )   -- Load Sample Data INSERT INTO TeamInfo VALUES ( 1, 'Jim' ) INSERT INTO TeamInfo VALUES ( 1, 'Mary' ) INSERT INTO TeamInfo VALUES ( 1, 'Bob' ) INSERT INTO TeamInfo VALUES ( 2, 'Sue' ) INSERT INTO TeamInfo VALUES ( 2, 'Ralph' ) INSERT INTO TeamInfo VALUES ( 2, 'Ellen' ) INSERT INTO TeamInfo VALUES ( 3, 'Bill' ) INSERT INTO TeamInfo VALUES ( 3, 'Linda' )
create function -- SQL 2000, Retrieve desired data
-- With SQL 2000, we will create a User Defined Function to do the concatenation. -- While this solution can also be used with SQL Server 2005/SQL Server 2008,  -- the previous suggestion is more efficient.   CREATE FUNCTION dbo.fnMakeTeamList    (  @TeamID int  )    RETURNS varchar(1000) AS    BEGIN       DECLARE @TempTeam table          (  Firstname varchar(20)  )       DECLARE @TeamList varchar(1000)       SET @TeamList = ''       INSERT INTO @TempTeam          SELECT FirstName          FROM TeamInfo          WHERE TeamID = @TeamID       IF @@ROWCOUNT > 0          UPDATE @TempTeam             SET @TeamList = ( @TeamList + FirstName + ', ' )       RETURN substring( @TeamList, 1, ( len( @TeamList ) - 1 ))    END   -- Usage SELECT     TeamID,     MemberList = dbo.fnMakeTeamList( TeamId )  FROM TeamInfo  GROUP BY TeamID   -- Results TeamID     MemberList 1 Jim, Mary, Bob 2 Sue, Ralph, Ellen 3 Bill, LindaS
QL Server 2005 / SQL Server 2008 Solution
--Retrieve desired data SELECT    t1.TeamID,    MemberList = substring((SELECT ( ', ' + FirstName )                            FROM TeamInfo t2                            WHERE t1.TeamID = t2.TeamID                            ORDER BY                                TeamID,                               FirstName                            FOR XML PATH( '' )                           ), 3, 1000 )FROM TeamInfo t1 GROUP BY TeamID   -- Results TeamID     MemberList 1 Bob, Jim, Mary 2 Ellen, Ralph, Sue 3 Bill, Linda