2015-04-11

TSQL - Get dayID, a date in the format YYYYMMDD

/*

       -- Execution
       select dbo.getDayID( '1/31/2012' );
       select dbo.getDayID( cast('1/31/2012' as date) );


*/
ALTER function [dbo].[getDayID](
       @PeriodDate date
)
returns bigint
as
begin

       return(
              ( year( @PeriodDate ) * 10000 )
              + ( month( @PeriodDate ) * 100 )
              + day(@PeriodDate)
       );

end
;


TSQL - Get the lower of two given integer numbers

/*


       -- Execution
       select dbo.getLowestInt( '201201', '201112' );
       select dbo.getLowestInt( '201201', null );
       select dbo.getLowestInt( null, '201201' );
       select dbo.getLowestInt( null, null );




*/
ALTER function [dbo].[getLowestInt](
       @date1 int
       , @date2 int
)
returns int
as
begin
       return(
              case
                     when isnull(@date1,0) <= isnull(@date2,0)
                     then @date1
                     else @date2
              end
       );
end
;



TSQL - Get the earlier of two given dates

/*


       -- Execution
       select dbo.getLowestDate( '1/31/2012', '8/30/2012' );
       select dbo.getLowestDate( '1/31/2012', null );
       select dbo.getLowestDate( null, '8/30/2012' );
       select dbo.getLowestDate( null, null );




*/
ALTER function [dbo].[getLowestDate](
       @date1 date
       , @date2 date
)
returns date
as
begin
       return(
              case when isnull(@date1,'12/31/2099') <= isnull(@date2,'12/31/2099') then @date1 else @date2 end
       );
end
;




TSQL - Get the greater of two given integer numbers

/*

       -- Execution
       select dbo.getGreatestInt( '201201', '201112' );
       select dbo.getGreatestInt( '201201', null );
       select dbo.getGreatestInt( null, '201201' );
       select dbo.getGreatestInt( null, null );


*/
ALTER function [dbo].[getGreatestInt](
       @date1 int
       , @date2 int
)
returns int
as
begin
       return(
              case
                     when isnull(@date1,0) >= isnull(@date2,0)
                     then @date1
                     else @date2
              end
       );
end
;






TSQL - Get the greater of two given dates

ALTER function [dbo].[getGreatestDate](
       @date1 date
       , @date2 date
)
returns date
as
begin
       return(
              case when isnull(@date1,'1/1/1900') >= isnull(@date2,'1/1/1900') then @date1 else @date2 end
       );
end
;

 g

TSQL - Format number with commas

/*

       -- Execution
       select dbo.getCommas( 1234.02 );
       select dbo.getCommas( -1234567.99 );


*/
ALTER function [dbo].[getCommas](
       @value as float
)
returns varchar(50)
as
begin

       return(
              replace(
                     CONVERT(varchar, CAST(round(@value,0) AS money), 1)
                     , '.00'
                     , ''
              )
       );

end
;




TSQL - Function to remove trailing zeros

ALTER function [dbo].[getNoTrailingZeroes]( @text varchar(max))
returns varchar(max)
as
begin
       return(
              reverse(
                     substring(reverse(@text), patindex('%[^0]%',reverse(@text)), 8000)
              )
       );
end;


TSQL - Function to remove leading zeros from a string

ALTER function [dbo].[getNoLeadingZeroes]( @text varchar(max))
returns varchar(max)
as
begin
       return(   substring(@text, patindex('%[^0]%',@text), 8000)   )
end;


TSQL - Function to remove HTML tags

ALTER FUNCTION dbo.getNoHTML
(
       @HTMLText VARCHAR(MAX)
)
RETURNS VARCHAR(MAX) AS
BEGIN
    DECLARE @Start INT
    DECLARE @End INT
    DECLARE @Length INT
    SET @Start = CHARINDEX('<',@HTMLText)
    SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
    SET @Length = (@End - @Start) + 1
      
       --removing ¿
       set @HTMLText = replace(@HTMLText, '¿','')

       --removing HTML <>
    WHILE @Start > 0 AND @End > 0 AND @Length > 0
    BEGIN
       if CHARINDEX('<',@HTMLText,CHARINDEX('<',@HTMLText)+1) between CHARINDEX('<',@HTMLText) and CHARINDEX('>',@HTMLText)
              begin
                     SET @HTMLText = STUFF(
                                                       @HTMLText
                                                       ,CHARINDEX('<',@HTMLText,CHARINDEX('<',@HTMLText)+1)
                                                       ,(CHARINDEX('>',@HTMLText)+1) - CHARINDEX('<',@HTMLText,CHARINDEX('<',@HTMLText)+1)
                                                       ,' ')
                     SET @Start = CHARINDEX('<',@HTMLText)
                     SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
                     SET @Length = (@End - @Start) + 1
              end
       else
              begin
              -- this is the good one
                     SET @HTMLText = STUFF(@HTMLText,@Start,@Length,' ')
                     SET @Start = CHARINDEX('<',@HTMLText)
                     SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
                     SET @Length = (@End - @Start) + 1
              end
    END



    RETURN LTRIM(RTRIM(@HTMLText))
END


2015-04-06

JavaScript - How to remove a property from an existing object

var myObject = { };
myObject["myProperty"] = {};
myObject["myProperty"]["mySubProperty"] = "mySubProperty value here.";

console.log( 'Before: ');
console.log( myObject );

console.log( 'After removing myProperty.mySubProperty:' );
delete myObject.myProperty.mySubProperty;
console.log( myObject );

Javascript - How to add properties to an existing object

var myObject = { };
myObject["myProperty"] = {};
myObject["myProperty"]["mySubProperty"] = "mySubProperty value.";
console.log( myObject );