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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Compare 12 hourse time formate

Author  Topic 

bekeer020
Starting Member

24 Posts

Posted - 2012-06-11 : 15:25:12
Dear all

I have column with nvarchar datatype
and the values like this

10:20 AM
2:31 PM

i want to compare this values with current time

Can any one help me ....

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-11 : 16:58:28
use cast() to cast it to time datatype and compare

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

shilpash
Posting Yak Master

103 Posts

Posted - 2012-06-11 : 17:01:59

CREATE TABLE #abc(tt NVARCHAR(20))

INSERT INTO #abc
(tt)
VALUES ('10:20 AM'
)


SELECT DATEDIFF (hh,convert(varchar(5),tt,8),convert(varchar(5),getdate(),8)) AS hours,DATEDIFF (mi,convert(varchar(5),tt,8),convert(varchar(5),getdate(),8)) AS mins FROM #abc
Go to Top of Page

bekeer020
Starting Member

24 Posts

Posted - 2012-06-11 : 17:44:28
Thanks all
i try to use cast then solved

=====
ALTER function [dbo].[check_class_now]
(
@Start_Time nvarchar(50),
@End_Time nvarchar(50)
)
returns nvarchar(50)
as
begin
declare @dStart_Time datetime
declare @dEnd_Time datetime
declare @Result nvarchar(50)

set @dStart_Time=(select cast(@Start_Time as datetime))
set @dEnd_Time=(select cast(@End_Time as datetime))

--select @mas1 , @mas2
if (dbo.gettimenow()>=@dStart_Time) and (dbo.gettimenow()<=@dEnd_Time)

set @Result='yes'


return @Result
END
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-12 : 15:17:15
Casting it to datetime causes datepart to be appended with base date value of 19000101. Hope you're aware of that

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -