Archive

Archive for January, 2009

Unix timestamp in C#

January 30, 2009 Leave a comment

Well here is a nice function to get the current unix timestap in C#:

public double unix_timestamp() {
    TimeSpan unix_time = (System.DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
    return unix_time.TotalSeconds;
}

Twit This Post!

Categories: C# Tags: ,

T-SQL Function to convert Unix Timestamp into MS SQL datetime

January 26, 2009 3 comments

Okay. Previously we converted datetime type values into unix timestamps. Now here is how to convert them back.

@datetime = DATEADD(second, @timestamp, {d '1970-01-01})

Where @timestamp is the unix timestamp we want to convert.
The {d ‘yyyy-mm-dd’} is an ODBC escape sequence.

Read more…

T-SQL DateTime to Unix Timestamp

January 26, 2009 5 comments

This is a really simple Transact-SQL function to make it easier to convert MS SQL’s datetime datatype to Unix Timestamps.

Converting a datetime to unix timestamp is easy, but involves error prone typing the following:

@timestamp=DATEDIFF(second,{d '1970-01-01'},@datetime)

Where @datetime is the datetime value you want to convert.
The {d ‘yyyy-mm-dd’} notation is an ODBC escape sequence.
Read more…

Getting the current date and time with Transact-SQL

January 19, 2009 Leave a comment

Recently I was looking for a way to get the current date and time through an SQL query in MS SQL Server 2005. I know that I can do that with the function now() in MySQL and PostgreSQL, but I couldn’t find this function for MS SQL. After digging around I found Matt Faus’s blog which had a post that wrote about the thing I wanted.

Matt Faus’s blog: MS SQL Server Transact-SQL NOW() Function

So to get the current datetime use:

SELECT GETDATE();
GO

Link to the MSDN article (SQL Server 2008): http://msdn2.microsoft.com/en-us/library/ms188383.aspx
Link to the MSDN article (SQL Server 2005): http://msdn.microsoft.com/en-us/library/ms188383(SQL.90).aspx

Also there is a function CURRENT_TIMESTAMP(). Usage is:

SELECT CURRENT_TIMESTAMP();
GO

Docs for this (SQL Server 2005)

Twit This Post!

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to Ma.gnoliaAdd to TechnoratiAdd to FurlAdd to Newsvine

New version of reset_sequence for PGSQL

January 15, 2009 Leave a comment

This is just an update of the previous PL/PGSQL function.

CREATE OR REPLACE FUNCTION "schemanema"."reset_sequence" (tablename text) RETURNS "pg_catalog"."void" AS
$body$
DECLARE 
   	idx integer DEFAULT 1;
    cmd varchar;
	
	BEGIN
		
    	EXECUTE 'SELECT id FROM "schemaname".'
        	|| tablename 
            || ' ORDER BY id DESC LIMIT 1' INTO idx;

		IF NOT FOUND THEN
        	idx = 1;
        ELSE
        	idx = idx + 1;
        END IF;    	
    
    	cmd = 'SELECT setval( ''schemaname.'
        	|| tablename
            || '_id_seq'', '
            || idx
            || ', false)';
               
	    EXECUTE cmd;
    END;
$body$
LANGUAGE 'plpgsql';

Collapse/Expand a HTML element made easy with jQuery

January 13, 2009 4 comments

Working with jQuery is really fun. I made a little handy plugin to make folding and unfolding parts of a page easier.

Read more…

Follow

Get every new post delivered to your Inbox.