![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
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 |
#2
| |||
| |||
|
|
-----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 . |
#3
| |||
| |||
|
|
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 . |
#4
| |||
| |||
|
|
-----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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |