Calendar

March 2010
S M T W T F S
« Feb    
 123456
78910111213
14151617181920
21222324252627
28293031  

Determining if a Date is a Weekday in T-SQL

create function fn_IsWeekDay
(
    @date datetime
)
returns bit
as
begin 

    declare @dtfirst int
    declare @dtweek int
    declare @iswkday bit 

    set @dtfirst = @@datefirst - 1
    set @dtweek = datepart(weekday, @date) - 1

    if (@dtfirst + @dtweek) % 7 not in (5, 6)
        set @iswkday = 1 --business day
    else
        set @iswkday = 0 --weekend

    return @iswkday
end

source http://ryanfarley.com

Hacking Java ClassLoader

เนื่องจากว่าการทำงานส่วนมาก ต้องเขียน Business Logic ที่ Store Procedure ใน MS-SQL ทำให้เกิดความลำบากกับการทำ version ของโค๊ดเป็นอย่างมาก เลยอยากจะเขียน Script สั่นๆสักตัวเพื่อดูด Store Procedure มาเก็บเป้นไฟล์ให้ แล้วเราจะได้ดูสิ่งที่ต่างกัน (Diff) และเก็บลง SVN Server ส่วนตัว (ที่ทำงานใช้ CVS ซึ่งไม่ชอบเอาซะเลย)

ก็เลยกะว่างานนี้เขียนสั่นๆ เอา Groovy ดีกว่า ก็เลยเริ่มจาก start groovy console (run>cmd>groovyConsole ) จากนั้นก็เริ่มเขียน code

ทันใดนั้นนึงขึ้นมาได้ว่า กูจะ add jar ยังไงว่ะ ไม่ได้ start เป็น project เอ้ หรือว่า จะไปสร้าง classpath ยังไงละเนี่ย ก็เลยลองไปหาว่าเจ้า Groovy Console มันจะ add classpath ได้ไงบ้าง

ไปเจอที่บอกว่าให้แก groovy start แต่เอ้ มันไม่เท่อ่ะ ไหน groovy บอกว่าตัวเองเป็น dynamic ก็นึกว่ามันจะเอา env ข้างๆมาให้ด้วย เลยเอา jar ไปวางใน Folder ที่ทำการสั่ง groovyConsole (ในใจนึงว่ามันจะเหมือน grails console) เอ๋ ไม่ได้นี่นา

ไม่ไหวแระ ไหนๆก็ไหนๆ เลยลองเขียน ให้ JAVA มันโหลด JAR แบบ on the fly เลยละกัน ไม่อยากเขียน cmd ไป เรียก class

Continue reading "Hacking Java ClassLoader" »

[TSQL]How to get list of Table in MS-SQL

USE [TABLE_NAME]
SELECT *
FROM sys.Tables
ORDER BY NAME

List all the Stored Procedures of a Database and their Definitions using T-SQL in SQL Server 2005/2008

SELECT obj.Name as SPName,

modu.definition as SPDefinition,

obj.create_date as SPCreationDate

FROM sys.sql_modules modu

INNER JOIN sys.objects obj

ON modu.object_id = obj.object_id

WHERE obj.type = ‘P’

Android Audio Recording Tutorial

package com.benmccann.android.hello;

import java.io.File;
import java.io.IOException;

import android.media.MediaRecorder;
import android.os.Environment;

/**
 * @author <a href="http://www.benmccann.com">Ben McCann</a>
 */
public class AudioRecorder {

  final MediaRecorder recorder = new MediaRecorder();
  final String path;

  /**
   * Creates a new audio recording at the given path (relative to root of SD card).
   */
  public AudioRecorder(String path) {
    this.path = sanitizePath(path);
  }

  private String sanitizePath(String path) {
    if (!path.startsWith("/")) {
      path = "/" + path;
    }
    if (!path.contains(".")) {
      path += ".3gp";
    }
    return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
  }

  /**
   * Starts a new recording.
   */
  public void start() throws IOException {
    String state = android.os.Environment.getExternalStorageState();
    if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
        throw new IOException("SD Card is not mounted.  It is " + state + ".");
    }

    // make sure the directory we plan to store the recording in exists
    File directory = new File(path).getParentFile();
    if (!directory.exists() && !directory.mkdirs()) {
      throw new IOException("Path to file could not be created.");
    }

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(path);
    recorder.prepare();
    recorder.start();
  }

  /**
   * Stops a recording that has been previously started.
   */
  public void stop() throws IOException {
    recorder.stop();
    recorder.release();
  }

}

source http://www.benmccann.com