Category Archives: SQL Server

Combining Date and Time in DTS To Make a DT_DBTIMESTAMP datetime

I was recently dealing with importing a DBISAM (Btrieve) database to SQL Server, which had separate date and time columns where I wanted to import a DateTime.  Using the ODBC driver and a DataReader data source, it was returning the date as the DT_DBTIMESTAMP type and the time as a DT_I8 long integer.

I tried using the Derived Column transformation to combine them, but never could get it to work.  There simply aren’t many date functions supported by the Derived Column formulas.  Finally, I settled on using the following VBS script using the Script Component transformation:

If Not Row.StartDate_IsNull Then
    If Row.StartTime_IsNull Then
        Row.StartDateTime = Row.StartDate
    Else
        Row.StartDateTime = Row.StartDate.AddTicks(Row.StartTime)
    End If
End If

Creating GUID Derived Column in DTS

If you want to add a constant GUID to a table during a DTS transformation, it’s a little tricky.  Here’s the solution.

Use the Derived Column transformation in order to add a new column.  For the formula, use the GUID surrounded by double quotes.  The trick is that you MUST include the curly braces, like so:

“{B83030CF-EC5A-45ca-BE2D-BCFCC2A85034}”

Then set the type of the column to unique identifier (DT_GUID).  That’s it.

Extracting The Date From a SQL DateTime In SQL Queries

Ever wondered how to quickly and easily get the date portion of a datetime field in a SQL query? Seems like it should be easy, right? Unfortunately, it isn’t. I’ve found a lot of solutions, but this one is the most elegant I’ve seen.

CONVERT(datetime, FLOOR(CONVERT(float, DateTime)))

I’ve put this into a user-defined function below:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[ExtractDate]
(
@DateTime datetime
)
RETURNS datetime
AS
BEGIN
RETURN CONVERT(datetime, FLOOR(CONVERT(float, @DateTime)))
END

This can be called like so:

SELECT dbo.ExtractDate(DateTime) FROM Table

Credit for this conversion method goes to Matt Berseth, i stumbled across it in the example code for something else.

SQL Server 2005 Service Broker Error Code 25 (Event ID 28054)

Another issue I’ve run into with the service broker and moving databases between servers is this error message:

Error code:25. The master key has to exist and the service master key encryption is required.

The solution that I found was the regenerate the key for the database using the following code:

CREATE MASTER KEY ENCRYPTION BY PASSWORD = ‘1ComplexPassword!’

You might also need to run the following afterwards:

ALTER DATABASE dbname SET NEW_BROKER.

Also, see my previous post SQL Server 2005 Service Broker Error 15517 for another error message and solution.

SQL Server 2005 Service Broker Error 15517

Sometimes I’ve run into the following error in the event log when using the service broker to do SQL command notifications with SQL 2005:

An exception occurred while enqueueing a message in the target queue. Error: 15517, State: 1. Cannot execute as the database principal because the principal “dbo” does not exist, this type of principal cannot be impersonated, or you do not have permission.

It tends to fill up the event log pretty badly when you see it. I’ve especially seen it when I move the database from one server to another. To fix it, use this command:

ALTER AUTHORIZATION ON DATABASE::[dbname] TO [SA]

This should fix it for you.