Archive for February, 2011

JavaScript Tips

Validating number (supports both integer and float type)
=============================================

value = jQuery.trim($(“#max_value”).val());

if(value * 1 != value ){
alert(“Maximum value should be a number.”);
}

Leave a Comment

PHP Tips

Log function
===========

function log_entry($log_str){
    $file = fopen("email_trigger.log", "a+");
    if($file)
    {
        fprintf($file, "\n[".date("Y-m-d H:i:s")."]".$log_str);
        fclose($file);
    }
}

SimpleXML – Removing node
==========================

$data='<data>
    <seg id="A1"/>
    <seg id="A5"/>
    <seg id="A12"/>
    <seg id="A29"/>
    <seg id="A30"/>
</data>';
$doc=new SimpleXMLElement($data);
foreach($doc->seg as $seg)
{
    if($seg['id'] == 'A12') {
        $dom=dom_import_simplexml($seg);
        $dom->parentNode->removeChild($dom);
    }
}
echo $doc->asXml();

Leave a Comment

MSSQL Tips

Listing all user created table names
============================

select name from dbname..sysobjects where xtype = ‘U’;

—————————————————————

USE YourDBName
SELECT * FROM sys.Tables

—————————————————————

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ‘BASE TABLE’

Listing index details of a table
======================

EXEC sp_helpindex ‘tablename’

Listing columns of a table
===================

1. select * from information_schema.columns where TABLE_NAME = ‘tablename’
2. exec sp_columns ‘tablename’


Grouping by week
=============

select DATEPART (wk, date_column) as WeekNum, count(*) as total
from table_name group by DATEPART (wk, date_column)

Above query gives results based on week numbers (1,2,3… 52)

Sample output
===========
1 – 30293
2 – 343423
3 – 897902



52 – 379829

select date_column – (DATEPART(DW, date_column) – 1) as week_start_date, count(*) as total
from table_name group by date_column – (DATEPART(DW, date_column) – 1)

Sample output
===========
2010-01-02 – 30293
2010-01-09 – 343423
2010-01-16 – 897902



2010-12-25 – 379829

NOTE:

=====

Below condition can be added to where clause if we want to omit results of current week, because current week has not yet completed.

Declare @todaydate smalldatetime = GetDate();

date_column – (DATEPART(DW,  date_column) – 1)  !=  convert(date, @todaydate- (DATEPART(DW,  @todaydate)- 1))

 

Leave a Comment