SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Compare 12 hourse time formate
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bekeer020
Starting Member

USA
24 Posts

Posted - 06/11/2012 :  15:25:12  Show Profile  Reply with Quote
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

India
47173 Posts

Posted - 06/11/2012 :  16:58:28  Show Profile  Reply with Quote
use cast() to cast it to time datatype and compare

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

Go to Top of Page

shilpash
Yak Posting Veteran

72 Posts

Posted - 06/11/2012 :  17:01:59  Show Profile  Reply with Quote

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

USA
24 Posts

Posted - 06/11/2012 :  17:44:28  Show Profile  Reply with Quote
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

India
47173 Posts

Posted - 06/12/2012 :  15:17:15  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000