Question : Convert Integer to time format in SQL Server 2005

I have a file that gets exported from an Excel spreadsheet.  The time in the spreadsheet is saved in 24 hour format as a number in Excel (ex: 0900 or 1530).  When import SQL simply sees these values as numbers.  I need to convert these numbers the standard time format SQL uses so I can store it with a date in a DATE/TIME field. The 0900 would then have a value of 9:00:00 AM and 1530 would have a value of 3:30:00 PM.  Thanks.

Answer : Convert Integer to time format in SQL Server 2005

I apologize for the long delay in getting back to this post; I have been gone for a while.  With the other solutions I was getting different types of errors; one solution yielded no results.  Below is the function I had to end up writing that works .  It may be a little complex for the task but, it works all of the time for what I'm looking for.  Thanks anyway for your time and effort experts.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
ALTER FUNCTION [dbo].[fnConvertIntegerToTime]
	(@ArrivalTime24hr int)
	
RETURNS varchar(12)
 
AS
	BEGIN
		DECLARE @ArrivalHour int,
			@ArrivalMinute int,
			@ArrivalTime varchar(12),
			-- @ArrivalTime24HrChr varchar(4),
			@ArrivalTime24HrLen int
			
	SET @ArrivalTime24HrLen = LEN(@ArrivalTime24hr)
	-- SET @ArrivalTime24HrChr = CAST(@ArrivalTime24hr AS varchar(2))
 
	IF @ArrivalTime24HrLen = 0
		BEGIN
			SET @ArrivalHour = 00
			SET @ArrivalMinute = 00
		END
	ELSE
 
	IF @ArrivalTime24HrLen > 0 AND @ArrivalTime24HrLen < 3
		BEGIN
			SET @ArrivalHour = 00
			SET @ArrivalMinute = @ArrivalTime24hr
		END
	ELSE
	
	IF @ArrivalTime24HrLen = 3
		BEGIN
			SET @ArrivalHour = LEFT(@ArrivalTime24hr,1)
			SET @ArrivalMinute = RIGHT(@ArrivalTime24hr,2)
		END
	ELSE
 
	IF @ArrivalTime24HrLen = 4
		BEGIN
			SET @ArrivalHour = LEFT(@ArrivalTime24Hr,2)
			SET @ArrivalMinute = RIGHT(@ArrivalTime24Hr,2)
		END
IF @ArrivalTime24hr < 1200 
	BEGIN
 
		SET @ArrivalTime =  CAST(@ArrivalHour AS char(2)) + ':'+ CAST(@ArrivalMinute AS char(2)) + ':00 AM'	
	END
 
	ELSE
	IF @ArrivalTime24hr BETWEEN 1200 AND 1259
	BEGIN
		SET @ArrivalTime =  CAST(@ArrivalHour AS char(2)) + ':'+ CAST(@ArrivalMinute AS char(2)) + ':00 PM'
 
	END	
	ELSE
	IF @ArrivalTime24hr BETWEEN 1300 AND 2359
	BEGIN
		SET @ArrivalTime =  CAST(@ArrivalHour - 12 As char(2)) + ':'+ CAST(@ArrivalMinute AS char(2)) + ':00 PM'
 
 
	END	
 
RETURN @ArrivalTime
END
Open in New Window Select All
Random Solutions  
 
programming4us programming4us