Thursday, September 10, 2009

Command Prompt Here Registry Hack

Here's how to get a "Command Prompt Here" action added to Windows Explorer so that it will drop to a cmd.exe prompt on any folder you have navigated to.

Copy/paste the below code segment. Save it as CmdPromptHere.reg somewhere, right click on it and choose "Merge" and OK the changes it will make to your registry.

-- CUT ----------------------------------------------

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\Command]
@="Command &Prompt Here"
[HKEY_CLASSES_ROOT\Directory\shell\Command\command]
@="cmd.exe"

-- CUT ---------------------------------------------

This is a simple operation that I end up doing to every machine I have and use.

-shannon norrell

Wednesday, September 9, 2009

Pervasive SQL PSQL v10 Custom Functions

I am having to struggle with hooking into a Pervasive SQL (Btrieve) database and have developed several custom functions that I thought I would share, as I did not find the answers on the internet forums and had to develop them myself. Perhaps I can save someone else a few hours of frustration.

The first, "PadWithZeroes" answers a question posed by many users

----------------------------------------------------------------------------------------------
-- PadWithZeroes(:N Integer, :NZ Integer) - Pad with Zeroes
-- Takes integer :N and returns a string padded with integer :NZ number of zeroes
----------------------------------------------------------------------------------------------
DROP FUNCTION PadWithZeroes;
CREATE FUNCTION PadWithZeroes(:N Integer, :NZ Integer)
RETURNS VARCHAR(25)
AS
BEGIN
DECLARE :retVal VARCHAR(25)
DECLARE :nString VARCHAR(25)
SET :nString = CONVERT(:N,SQL_CHAR)
IF LENGTH(:nString)>:NZ THEN
SET :retVal = :nString;
ELSE
SET :retVal = CONCAT(
RIGHT('0000000000000',:NZ - LENGTH(:nString)),
:nString
);
END IF
RETURN :retVal;
END;

The second ConvertToFOSDate, takes a date stored a long integer which is, in fact the number of days offset from January 1st, 1900 (aka a Julian Date) and converts it into a usable Date object:

----------------------------------------------------------------------------------------------
-- ConvertToFOSDate(:D integer) - converts a date stored like 40026 and converts it to a date
-- Takes NumberOfDays :D since 1900 to get the Date
----------------------------------------------------------------------------------------------
DROP FUNCTION ConvertToFOSDate;
CREATE FUNCTION ConvertToFOSDate(:D integer)
RETURNS DATE
AS
BEGIN
DECLARE :theDate DATE;
SET :theDate = DATEADD(DAY,:D,CONVERT('1899-12-31', SQL_DATE));
RETURN :theDate;
END;

The third takes a number of minutes and returns a string formatted as HH:MM

----------------------------------------------------------------------------------------------
-- ConvertToHHMM(:T integer)
-- Takes NumberOfMinutes :T and returns a string formatted as HH:MM
----------------------------------------------------------------------------------------------
DROP FUNCTION ConvertToHHMM;
CREATE FUNCTION ConvertToHHMM(:T integer)
RETURNS VARCHAR(5)
AS
BEGIN
DECLARE :theTime VARCHAR(5)
IF (:T = 0) THEN
SET :theTime = '00:00';
ELSE
SET :theTime = CONCAT(
CONCAT(PadWithZeroes(CONVERT(FLOOR(:T/60),SQL_INTEGER),2), ':'),
PadWithZeroes(CONVERT(ROUND(MOD(:T,60)/60,1)*60,SQL_INTEGER),2)
);
END IF;
RETURN :theTime;
END;

Here are a couple more. They are not world changing, but may be of help to someone:

----------------------------------------------------------------------------------------------
-- GetTimeInHundredths(:A integer)
-- Returns Minutes as a percentage of a day
----------------------------------------------------------------------------------------------
DROP FUNCTION GetTimeInHundredths;
CREATE FUNCTION GetTimeInHundredths(:A integer)
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE :theTime VARCHAR(10)
IF (:A = 0 ) THEN
SET :theTime = '0';
ELSE
SET :theTime =
CONCAT(
CONCAT( CONVERT( FLOOR(:A/60), SQL_CHAR), '.'),
CONVERT( ROUND(MOD(:A,60)/60,1)*10, SQL_CHAR)
);
END IF;
RETURN :theTime;
END;
----------------------------------------------------------------------------------------------
-- MinutesBetween(:D1 DateTime, :D2 DateTime)
-- Computes the integer numberOfMinutes between Date1 :D1 and Date2 :D2
----------------------------------------------------------------------------------------------
DROP FUNCTION MinutesBetween;
CREATE FUNCTION MinutesBetween(:D1 DateTime, :D2 DateTime)
RETURNS INTEGER
AS
BEGIN
DECLARE :minutesBetween INTEGER;
SET :minutesBetween = CONVERT( DATEDIFF(MINUTE, :D1, :D2), SQL_INTEGER);
RETURN :minutesBetween;
END;


-shannon norrell