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 2012 Forums
 Transact-SQL (2012)
 Help compairing the time in DateTime to VARCHAR.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Eagle_f90
Constraint Violating Yak Guru

USA
424 Posts

Posted - 07/26/2013 :  10:14:01  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
I am trying to update a proc and need to Compare a DATETIME variable named "StartDate" which contains a full Date and Time in 24 hour format. This variable needs to have it's time component compared to a table column (named "StrTm") that is VARCHAR(10) and holds only a 4 digit number 0000 - 2359 to represent a 24 hour time format. I know I can use CONVERT(VARCHAR(10), @StartDate, 114) to get the Hour and minutes in a 24 hour format but that also gives me seconds, milliseconds, and the : separator. All of which make the comparison to the StrTm column not work. What is the easiest way to extract just the hour and minutes in a 24h format with no separators so I can properly compare @StartDate to StrTm?

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

James K
Flowing Fount of Yak Knowledge

3707 Posts

Posted - 07/26/2013 :  10:29:14  Show Profile  Reply with Quote
Take only the parts you want from what you converted, for example using LEFT and REPLACE like this:
REPLACE(LEFT(CONVERT (VARCHAR(10), @StartDate, 114),5),':','')
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

USA
424 Posts

Posted - 07/26/2013 :  10:30:14  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
Thanks, that looks to be working quite well.

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3707 Posts

Posted - 07/26/2013 :  10:36:18  Show Profile  Reply with Quote
You are very welcome.

Before someone else does it let me take this opportunity to be harsh on you for storing time in a string column. I don't want to describe the reasons, I want to let someone else do that ;)
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 07/28/2013 :  10:02:19  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
From 2008 onwards you can make use of TIME datatype to store time values

Madhivanan

Failing to plan is Planning to fail
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.05 seconds. Powered By: Snitz Forums 2000