Stored Procedure Problem

They have: 82 posts

Joined: Oct 2001

OK Guys,

I am working with some C++ guys from the 70's.

They have dates in a SQL server Database as ints.

I need to take the Current Time (military) and convert to this
format:

12:06:36 PM = 120636

The Format is: 120636

The Problem is I get : 126 36

The proc removes the 0's.

Any ideas on how to overcome this?

Thanks,

Mike

CODE BELOW ******************************************************

PRINT 'Creating PROCEDURE ap_TimeTest...'
GO

IF EXISTS (SELECT * FROM sysobjects WHERE name = 'ap_TimeTest')
DROP PROC ap_TimeTest
GO

Create Procedure ap_TimeTest
as
Set NOCOUNT On
Declare @Hour as char(2),
@Minute as char(2),
@Second as char(2),
@LocalTime as char(Cool,
@CurrentTime as DateTime,
@LocalDate as Char(4),
@Day as char(2),
@Month as char(2)

--- GET CURRENT DATE AND ASSIGN TO @CurrentTime
select @CurrentTime=getdate(),

-- BREAK UP INTO HOUR MINUTE SECOND AND MONTH DAY
@Hour=DATEPART(hh, @CurrentTime),
@Minute=DATEPART(mi, @CurrentTime),
@Second=DATEPART(ss, @CurrentTime)
Set @Hour=CAST(@Hour AS Char(2))
Set @Minute=CAST(@Minute AS Char(2))
Set @Second=CAST(@Second AS Char(2))

-- SET LOCAL TIME -------------------------
Set @LocalTime=(@Hour+@Minute+@second)

-- ACTAULLY GET MONTH AND DAY -------------
Select @Day=DATEPART(dd, @CurrentTime),
@Month=DATEPART(mm,@CurrentTime)

-- SET LOCALDATE
Set @LocalDate=(@Month+@Day)

Select @LocalDate as 'Local Date'
Select @LocalTime as 'Local Time'
Select GetDate() as 'Date Time'

Blessed is the man who fears the LORD, who delights greatly in his commandments. Psalms 112:1

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

Check the date/time format on the server, I'm pretty sure SQL takes its settings from there, you might be able to force the 0.

If not just add the following line after every calculation:

Set @Hour=CAST(@Hour AS Char(2))
If Len(@Hour) = 1 Set @Hour = '0' + @Hour

Repeat for minutes and seconds. Not pretty but it should do the trick.

PJ | Are we there yet?
pjboettcher.com

They have: 82 posts

Joined: Oct 2001

Thanks Peter!

I ended checking the length of the
variable and using an STR() function then
concatenating.

I could post the code tomorrow if you would like to see it.

Thanks,

Mike

Blessed is the man who fears the LORD, who delights greatly in his commandments. Psalms 112:1

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.