Solved

Changing the time value of a sql datetime

Posted on 2007-11-16
5
4,070 Views
Last Modified: 2008-02-01
Hi How can I set just the time value of a datetime:

for example

DECLARE @ENDDATE SMALLDATETIME
SET @ENDDATE = GETDATE()

If i wanted to then amend the time value of @ENDDATE, how would I do this?
0
Comment
Question by:scm0sml
  • 3
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 20297597
select CONVERT(varchar,getdate(), 108)
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 20297602
make sure that you store that value in a Varchar column
declare @Time = varchar(10)
select @Time = CONVERT(varchar,getdate(), 108)
0
 

Author Comment

by:scm0sml
ID: 20297618
say i have

SET @TESTDATE = GETDATE()

That will give me the date and time for now.

But say I wanted to set the time value of @TESTTIME to 09:30 for example.

How can i do this?
0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 100 total points
ID: 20297621

DECLARE @ENDDATE SMALLDATETIME
SET @ENDDATE = GETDATE()
 
set @change smalldatetime set @change = '12:30:12'
set @endDate = convert(datetime,
 convert(varchar, @endDate, 102) + ' ' + convert(varchar, @change, 8))

Open in new window

0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 400 total points
ID: 20297633
scm0sml,



Aneesh R

select CAST(CONVERT(varchar,getdate(), 112)   +' ' +'09:30'AS DATETIME)

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question