Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dim
Yak Posting Veteran
57 Posts |
Posted - 2010-09-21 : 15:18:02
|
| Hi,I have a database column which is of type varchar and gives values like 07:10a,06:55p,09:30p....etcI need to covert these into Military Time Format like 07:10a -----should be 07:1006:55p------should be 18:55I am not able to use the standard conversion function as these values have a or p attached. Please let me know if there is a way to achieve this.Thank you,Dp |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-09-21 : 15:41:57
|
now this is just plain silly but...DECLARE @t table(t varchar(10)); INSERT INTO @t(t) SELECT '06:55p' UNION ALL SELECT '06:55a' SELECT CONVERT(varchar(25), CONVERT(datetime, RIGHT('0' + CONVERT(varchar(2),CONVERT(int,SUBSTRING(t,1,2)) + CASE WHEN SUBSTRING(t,6,1) = 'p' THEN 12 ELSE 0 END),2) + SUBSTRING(t,3,3)) , 108)FROM @tBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspx |
 |
|
|
dim
Yak Posting Veteran
57 Posts |
Posted - 2010-09-21 : 16:11:45
|
| Hi X002548I tried your code and I am able to get tge desired output with static values. But when I use the column ---I get the error messageThe conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.My code is as follows:SELECT substring (CONVERT(varchar(25), CONVERT(datetime, RIGHT('0' + CONVERT(varchar(2),CONVERT(int,SUBSTRING(dbo.Driver_Actual_Out,1,2)) + CASE WHEN SUBSTRING(dbo.Driver_Actual_Out,6,1) = 'p' THEN 12 ELSE 0 END),2) + SUBSTRING(dbo.Driver_Actual_Out,3,3)) , 108), 1, 5)from dbo.DriverIn the column I do not have any out of range time values ...it is very natural like 07:55p , 09:00a, 06:30p .....Thank You, Dp |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2010-09-21 : 16:33:41
|
| [code]declare @t table (t varchar(10)); insert into @t (t)select '06:55p' union allselect '06:55a' select t, [DateTime] = convert(datetime,t+'m')from @t[/code]Results:[code](2 row(s) affected)t DateTime---------- -----------------------06:55p 1900-01-01 18:55:00.00006:55a 1900-01-01 06:55:00.000(2 row(s) affected)[/code]CODO ERGO SUM |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-09-21 : 16:45:40
|
dude....there are no miraclessome of your data does not fit your modelrun this...just replace the table and column nameDECLARE @t table(t varchar(10)); INSERT INTO @t(t) SELECT '06:55p' UNION ALL SELECT '06:55a' UNION ALLSELECT '0X:55p' UNION ALL SELECT '06:55c' UNION ALLSELECT '06:5Zp' SELECT t , ISNUMERIC(SUBSTRING(t,1,2)), ISNUMERIC(SUBSTRING(t,4,2)) , SUBSTRING(t,6,1), SUBSTRING(t,3,1) FROM @t WHERE ISNUMERIC(SUBSTRING(t,1,2)) = 0 OR ISNUMERIC(SUBSTRING(t,4,2)) = 0 OR SUBSTRING(t,6,1) NOT IN ('a','p') OR SUBSTRING(t,3,1) <> ':'Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspx |
 |
|
|
dim
Yak Posting Veteran
57 Posts |
Posted - 2010-09-21 : 17:04:34
|
| Thank You I had spaces in the data and as a result I was getting the error. Applying the ltrim and case statement I was able to execute the above query. Thank You,Dp |
 |
|
|
|
|
|
|
|