dbTalk Databases Forums  

Re: Converting String to Datetime

microsoft.public.sqlserver.mseq microsoft.public.sqlserver.mseq


Discuss Re: Converting String to Datetime in the microsoft.public.sqlserver.mseq forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Vishal Parkar
 
Posts: n/a

Default Re: Converting String to Datetime - 07-18-2003 , 10:40 AM






if you post your exact select/insert statement with the format you are using
it would be possible to give correct solution. By the way following are the
tips that you can use.

You can SET DATEFORMAT and modify code as follows.

create table test(dt datetime)
go
declare @dt varchar(32)
set @dt ='26/07/2002'
set dateformat dmy --change dateformat
insert into test values(convert(datetime,@dt)) ;
select * from test;
go

OR other method would be to use explicit coversion of the
date using CONVERT function.

create table test(dt datetime)
go
declare @dt varchar(32)
set @dt ='26/07/2002'
insert into test values(convert(datetime,@dt, 103)) ; --103 stands for the
format dd/mm/yyyy
select * from test;
go

--
-Vishal

"Carmen" <cjohnston (AT) pbsj (DOT) com> wrote

Quote:
HELP! Need to convert a string to Datetime format. Tried
using Cast and Convert, but not working.

error message:
Server: Msg 241, Level 16, State 1, Line 31
Syntax error converting datetime from character string.

Thanks a million!
CJ

Here's part of what I'm doing:

declare
@StartDate varchar(40),
@EndDate varchar(40)

set @StartDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-16-0' + rtrim((convert(char(3),
@PrevMonth))) + ' 00:00:00:000'''
set @EndDate = '''' + rtrim((convert(char(4),
@CuurrentYear))) + '-01-0' + rtrim((convert(char(3),
@CurrentMonth))) + ' 00:00:00:000'''

select @StartDate + @EndDate
select cast(@StartDate as datetime), cast(@EndDate
as datetime)
select convert(datetime, @StartDate, 101)
end





Reply With Quote
  #2  
Old   
Carmen
 
Posts: n/a

Default Re: Converting String to Datetime - 07-18-2003 , 11:05 AM






Here's the exact script I'm using.

This is just to calculate the date. The calculated date
is then being used in another query to gather information.

Thanks again!
CJ

-- Date Calc Script

declare
@Today datetime,
@CurrentMonth int,
@CurrentYear int,
@PrevMonth int,
@StartDate varchar(40),
@EndDate varchar(40)

set @Today = getdate()
set @CurrentMonth = DATEPART(month, @Today)
set @CurrentYear = DATEPART(year, @Today)
set @PrevMonth = DATEPART(month, @Today) -1

select @CurrentMonth as 'CurrentMonth', @CurrentYear
as 'CurrentYear', @PrevMonth as 'PrevMonth'
select 'Today is :' + convert(char(20), @Today)

if (DATEPART(day, @Today) between 1 and 15)
begin
set @StartDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-16-0' + rtrim((convert(char(3),
@PrevMonth))) + ' 00:00:00:000'''
set @EndDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-01-0' + rtrim((convert(char(3),
@CurrentMonth))) + ' 00:00:00:000'''
select 'Between 1 and 15'
select cast(@StartDate as datetime), cast(@EndDate
as datetime)
select @StartDate + @EndDate
end
else
begin
set @StartDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-01-0' + rtrim((convert(char(3),
@CurrentMonth))) + ' 00:00:00:000'''
set @EndDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-16-0' + rtrim((convert(char(3),
@CurrentMonth))) + ' 00:00:00:000'''
select 'Between 16 and 31'
select @StartDate + @EndDate
select cast(@StartDate as datetime), cast(@EndDate
as datetime)
select convert(datetime, @StartDate, 101)
end


-- Second query that gathers information based on
calculated date:
....
where EecEEID = EepEEID
and EecLocation = LocCode
and EecEmplStatus <> 'T'
and EecDateOfLastHire >= @StartDate
and EecDateOfLastHire <= @EndDate




Quote:
-----Original Message-----
if you post your exact select/insert statement with the
format you are using
it would be possible to give correct solution. By the way
following are the
tips that you can use.

You can SET DATEFORMAT and modify code as follows.

create table test(dt datetime)
go
declare @dt varchar(32)
set @dt ='26/07/2002'
set dateformat dmy --change dateformat
insert into test values(convert(datetime,@dt)) ;
select * from test;
go

OR other method would be to use explicit coversion of the
date using CONVERT function.

create table test(dt datetime)
go
declare @dt varchar(32)
set @dt ='26/07/2002'
insert into test values(convert(datetime,@dt, 103)) ; --
103 stands for the
format dd/mm/yyyy
select * from test;
go

--
-Vishal

"Carmen" <cjohnston (AT) pbsj (DOT) com> wrote in message
news:0c3101c34d40$8a0fc1c0$a601280a (AT) phx (DOT) gbl...
HELP! Need to convert a string to Datetime format.
Tried
using Cast and Convert, but not working.

error message:
Server: Msg 241, Level 16, State 1, Line 31
Syntax error converting datetime from character string.

Thanks a million!
CJ

Here's part of what I'm doing:

declare
@StartDate varchar(40),
@EndDate varchar(40)

set @StartDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-16-0' + rtrim((convert(char(3),
@PrevMonth))) + ' 00:00:00:000'''
set @EndDate = '''' + rtrim((convert(char(4),
@CuurrentYear))) + '-01-0' + rtrim((convert(char(3),
@CurrentMonth))) + ' 00:00:00:000'''

select @StartDate + @EndDate
select cast(@StartDate as datetime), cast
(@EndDate
as datetime)
select convert(datetime, @StartDate, 101)
end




.


Reply With Quote
  #3  
Old   
Vishal Parkar
 
Posts: n/a

Default Re: Converting String to Datetime - 07-19-2003 , 09:20 AM



you are putting 2 additional double quotes unnecessarily
change your code as follows.

set @StartDate = rtrim((convert(char(4), @CurrentYear))) + '-01-0' +
rtrim((convert(char(3), @CurrentMonth))) + ' 00:00:00:000'
set @EndDate = rtrim((convert(char(4), @CurrentYear))) + '-16-0' +
rtrim((convert(char(3), @CurrentMonth))) + ' 00:00:00:000'

--
-Vishal

"Carmen" <cjohnston (AT) pbsj (DOT) com> wrote

Quote:
Here's the exact script I'm using.

This is just to calculate the date. The calculated date
is then being used in another query to gather information.

Thanks again!
CJ

-- Date Calc Script

declare
@Today datetime,
@CurrentMonth int,
@CurrentYear int,
@PrevMonth int,
@StartDate varchar(40),
@EndDate varchar(40)

set @Today = getdate()
set @CurrentMonth = DATEPART(month, @Today)
set @CurrentYear = DATEPART(year, @Today)
set @PrevMonth = DATEPART(month, @Today) -1

select @CurrentMonth as 'CurrentMonth', @CurrentYear
as 'CurrentYear', @PrevMonth as 'PrevMonth'
select 'Today is :' + convert(char(20), @Today)

if (DATEPART(day, @Today) between 1 and 15)
begin
set @StartDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-16-0' + rtrim((convert(char(3),
@PrevMonth))) + ' 00:00:00:000'''
set @EndDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-01-0' + rtrim((convert(char(3),
@CurrentMonth))) + ' 00:00:00:000'''
select 'Between 1 and 15'
select cast(@StartDate as datetime), cast(@EndDate
as datetime)
select @StartDate + @EndDate
end
else
begin
set @StartDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-01-0' + rtrim((convert(char(3),
@CurrentMonth))) + ' 00:00:00:000'''
set @EndDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-16-0' + rtrim((convert(char(3),
@CurrentMonth))) + ' 00:00:00:000'''
select 'Between 16 and 31'
select @StartDate + @EndDate
select cast(@StartDate as datetime), cast(@EndDate
as datetime)
select convert(datetime, @StartDate, 101)
end


-- Second query that gathers information based on
calculated date:
...
where EecEEID = EepEEID
and EecLocation = LocCode
and EecEmplStatus <> 'T'
and EecDateOfLastHire >= @StartDate
and EecDateOfLastHire <= @EndDate




-----Original Message-----
if you post your exact select/insert statement with the
format you are using
it would be possible to give correct solution. By the way
following are the
tips that you can use.

You can SET DATEFORMAT and modify code as follows.

create table test(dt datetime)
go
declare @dt varchar(32)
set @dt ='26/07/2002'
set dateformat dmy --change dateformat
insert into test values(convert(datetime,@dt)) ;
select * from test;
go

OR other method would be to use explicit coversion of the
date using CONVERT function.

create table test(dt datetime)
go
declare @dt varchar(32)
set @dt ='26/07/2002'
insert into test values(convert(datetime,@dt, 103)) ; --
103 stands for the
format dd/mm/yyyy
select * from test;
go

--
-Vishal

"Carmen" <cjohnston (AT) pbsj (DOT) com> wrote in message
news:0c3101c34d40$8a0fc1c0$a601280a (AT) phx (DOT) gbl...
HELP! Need to convert a string to Datetime format.
Tried
using Cast and Convert, but not working.

error message:
Server: Msg 241, Level 16, State 1, Line 31
Syntax error converting datetime from character string.

Thanks a million!
CJ

Here's part of what I'm doing:

declare
@StartDate varchar(40),
@EndDate varchar(40)

set @StartDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-16-0' + rtrim((convert(char(3),
@PrevMonth))) + ' 00:00:00:000'''
set @EndDate = '''' + rtrim((convert(char(4),
@CuurrentYear))) + '-01-0' + rtrim((convert(char(3),
@CurrentMonth))) + ' 00:00:00:000'''

select @StartDate + @EndDate
select cast(@StartDate as datetime), cast
(@EndDate
as datetime)
select convert(datetime, @StartDate, 101)
end




.




Reply With Quote
  #4  
Old   
Eric
 
Posts: n/a

Default Re: Converting String to Datetime - 07-21-2003 , 05:22 AM




Why are you converting the datetime values to Varchars?

Alternative method of calculating your dates:
declare
@Today datetime,
@StartDate smalldatetime,
@EndDate smalldatetime

--set @Today = getdate()
set @Today = LEFT ( CONVERT(datetime, getdate(),
103), 11 ) --This sets the time to '00:00:00'

--set @Today = '2003-07-12'
--set @Today = '2003-07-15'
--set @Today = '2003-12-12'
--set @Today = '2003-12-18'
--set @Today = '2004-01-12'
--set @Today = '2004-01-18'

if (DATEPART(day, @Today) between 1 and 15)
Begin
set @StartDate = DATEADD(month, -1, (DATEADD(day, (16 -
(DATEPART(day, @Today))), @Today)) )
set @EndDate = DATEADD(day, (1 - (DATEPART(day,
@Today))), @Today)
End
Else
Begin
set @StartDate = DATEADD(day, (1 - (DATEPART(day,
@Today))), @Today)
set @EndDate = DATEADD(day, (16 - (DATEPART(day,
@Today))), @Today)
End

select @StartDate as 'Start',
@EndDate as 'End'

Cheers,
Eric

Quote:
-----Original Message-----
Here's the exact script I'm using.

This is just to calculate the date. The calculated date
is then being used in another query to gather information.

Thanks again!
CJ

-- Date Calc Script

declare
@Today datetime,
@CurrentMonth int,
@CurrentYear int,
@PrevMonth int,
@StartDate varchar(40),
@EndDate varchar(40)

set @Today = getdate()
set @CurrentMonth = DATEPART(month, @Today)
set @CurrentYear = DATEPART(year, @Today)
set @PrevMonth = DATEPART(month, @Today) -1

select @CurrentMonth as 'CurrentMonth', @CurrentYear
as 'CurrentYear', @PrevMonth as 'PrevMonth'
select 'Today is :' + convert(char(20), @Today)

if (DATEPART(day, @Today) between 1 and 15)
begin
set @StartDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-16-0' + rtrim((convert(char(3),
@PrevMonth))) + ' 00:00:00:000'''
set @EndDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-01-0' + rtrim((convert(char(3),
@CurrentMonth))) + ' 00:00:00:000'''
select 'Between 1 and 15'
select cast(@StartDate as datetime), cast
(@EndDate
as datetime)
select @StartDate + @EndDate
end
else
begin
set @StartDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-01-0' + rtrim((convert(char(3),
@CurrentMonth))) + ' 00:00:00:000'''
set @EndDate = '''' + rtrim((convert(char(4),
@CurrentYear))) + '-16-0' + rtrim((convert(char(3),
@CurrentMonth))) + ' 00:00:00:000'''
select 'Between 16 and 31'
select @StartDate + @EndDate
select cast(@StartDate as datetime), cast
(@EndDate
as datetime)
select convert(datetime, @StartDate, 101)
end


-- Second query that gathers information based on
calculated date:
....
where EecEEID = EepEEID
and EecLocation = LocCode
and EecEmplStatus <> 'T'
and EecDateOfLastHire >= @StartDate
and EecDateOfLastHire <= @EndDate




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.