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
 General SQL Server Forums
 New to SQL Server Programming
 Converting datetime
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Pinto
Aged Yak Warrior

United Kingdom
590 Posts

Posted - 06/22/2011 :  10:05:29  Show Profile  Reply with Quote
I have a datetime field to store the time. My user wants to select the time in 15 minute intervals from a dropdownlist. So, I bind my (web) dropdownlist to this sp which displays it as 12:00, 12:15 etc etc

SELECT *, LEFT(right(CONVERT(VARCHAR(30),TI_Time,108),8),5) as "ShortTime" from tbl_Time

My question is, when I save the selected time back to the sql table how do I convert 12:00 back to 1900-01-01 12:00:00.000

robvolk
Most Valuable Yak

USA
15688 Posts

Posted - 06/22/2011 :  10:20:23  Show Profile  Visit robvolk's Homepage  Reply with Quote
Just convert it to datetime:

select cast('12:15' as datetime)

Here's a shorter query that does the same as what you posted:

select *, CONVERT(char(5), TI_Time, 8) [ShortTime] from tbl_Time

Go to Top of Page

Pinto
Aged Yak Warrior

United Kingdom
590 Posts

Posted - 06/22/2011 :  11:01:24  Show Profile  Reply with Quote
Thanks for that but do I convert it in my stored procedure which updates my table or in my asp.net code ?
Go to Top of Page

robvolk
Most Valuable Yak

USA
15688 Posts

Posted - 06/22/2011 :  11:27:44  Show Profile  Visit robvolk's Homepage  Reply with Quote
Probably easier to do in the stored procedure.
Go to Top of Page

jcelko
Esteemed SQL Purist

USA
547 Posts

Posted - 06/23/2011 :  00:39:12  Show Profile  Visit jcelko's Homepage  Reply with Quote
>> My question is, when I save the selected time back to the sql table how do I convert 12:00 back to 1900-01-01 12:00:00.000 <<

Did you know that we now have a TIME data type, so you don't have to this silly, expensive coding any more?

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
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