June 4, 2009 at 2:57 am
Hi guys..
I want to convert from datetime to time part only. Like this sample :
6/4/2009 3:50:06 PM to 3:50:06 PM
I really appreciate for the help
Thank,
YuzZ
June 4, 2009 at 3:21 am
declare @time datetime
select @time = getdate()
select @time, convert(char(8),@time,108) as OnlyTime1,
dateadd(dd, datediff(dd,@time,'19000101'),@time) as OnlyTime2
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
June 4, 2009 at 3:48 am
Thx for your help Adi.
But, if i want to create table like this :
CREATE TABLE [WeatherInfo] (
[WeatherID] [int] NOT NULL ,
[DateNow] [datetime] NOT NULL ,
[TimeNow] [datetime] NOT NULL ,
[Until] [datetime] NOT NULL ,
[Temperatur] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[Symbol] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
CONSTRAINT [PK_WeatherInfo] PRIMARY KEY CLUSTERED
(
[WeatherID]
) ON [PRIMARY]
) ON [PRIMARY]
GO
then i want insert some values :
INSERT INTO [PMCS].[dbo].[WeatherInfo]([WeatherID], [DateNow], [TimeNow], [Until], [Temperatur], [Symbol])
VALUES(, , , , , )
i want the result like this :
1 - 2009-06-04 - 16:29:23 - 18:29:23 - 30C - Hot
Thx before
June 4, 2009 at 3:55 am
Do you want it returned as datetime or as a string?
_____________
Code for TallyGenerator
June 4, 2009 at 4:13 am
As string... thx
June 4, 2009 at 4:14 am
Upss sorry.... as datetime
June 4, 2009 at 4:45 am
DATEADD(dd, DATEDIFF(dd, 0, DateTimeValue), 0)
_____________
Code for TallyGenerator
June 4, 2009 at 6:26 am
yuzzelpiccici (6/4/2009)
Thx for your help Adi.But, if i want to create table like this :
CREATE TABLE [WeatherInfo] (
[WeatherID] [int] NOT NULL ,
[DateNow] [datetime] NOT NULL ,
[TimeNow] [datetime] NOT NULL ,
[Until] [datetime] NOT NULL ,
[Temperatur] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[Symbol] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
CONSTRAINT [PK_WeatherInfo] PRIMARY KEY CLUSTERED
(
[WeatherID]
) ON [PRIMARY]
) ON [PRIMARY]
GO
then i want insert some values :
INSERT INTO [PMCS].[dbo].[WeatherInfo]([WeatherID], [DateNow], [TimeNow], [Until], [Temperatur], [Symbol])
VALUES(, , , , , )
i want the result like this :
1 - 2009-06-04 - 16:29:23 - 18:29:23 - 30C - Hot
Thx before
You can use the convert function. When you want to get the date part you can use style 101 and when you want to use the time part you can use style 108. Here is an example:
declare @StartDate datetime
declare @EndDate datetime
set @StartDate = '2009-06-04 16:29:23'
set @EndDate = '2009-06-04 18:29:23'
INSERT INTO [dbo].[WeatherInfo]([WeatherID], [DateNow], [TimeNow], [Until], [Temperatur], [Symbol])
VALUES(1, convert(char(10),@StartDate,101), convert(char(10),@StartDate,108), convert(char(10),@EndDate,108), '30C', 'Hot')
select * from [WeatherInfo]
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
June 4, 2009 at 9:18 am
Thx for the helps guys
June 4, 2009 at 3:09 pm
Adi,
conversion through char is the slowest and resource consuming way to do this.
Don't use it any more.
_____________
Code for TallyGenerator
Viewing 10 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy