Author |
Topic |
achilles
Starting Member
3 Posts |
Posted - 2006-10-02 : 11:33:19
|
Hi, 'am fairly very new to SQL SERVER 2000.I have this particular problem....I need to develop a stored procwhere in a parameter checks the dates between JAN 1st and DEC 31st of a particular year. The parameter is declared for year..I'll mention a small example below...USE PUBSdeclare @year as varcharset @year = 1993select * from employeewhere hire_date >= '@year-01-01'and hire_date<='@year-12-31'This is just an example to show wat i want...the year is prompted to the user...he/she can select any yearI get the following error message....Server: Msg 241, Level 16, State 1, Line 1Syntax error converting datetime from character string.Can anyone help me on this......Thanks in advance |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-02 : 11:41:05
|
select * from employeewhere hire_date >= @year+'-01-01'and hire_date<=@year+'-12-31'Why dont you use DATETIME parameter?MadhivananFailing to plan is Planning to fail |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-10-02 : 12:02:07
|
You should use "date constants" which are in the format "yyyymmdd" - you would be better to avoid the hyphens in the example you give - so I would suggest:select * from employeewhere hire_date >= @year + '0101'and hire_date <= @year + '1231'Note also thatand hire_date <= @year + '1231'is going to EXCLUDE anything on 31st December with a time AFTER midnight. This is only a problem if you are storing a Time as well as a Date, but that will be the case if you are, for example, using the SQL system function GetDate() to provide the date for your hire_date.So, again using a broken-syntax example, you might be better to change:and hire_date <= @year + '1231'toand hire_date < CONVERT(varchar(20), @year+1) + '0101'Kristen |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-10-02 : 12:23:34
|
[code]select *from MyTablewhere -- GE start of this year hire_date >= dateadd(yy,@year-1900,0) and -- LT start of next year hire_date < dateadd(yy,@year-1899,0)[/code]CODO ERGO SUM |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-10-02 : 12:36:29
|
Yeah, much better. Too easy to get sucked into string handling for dates ... |
|
|
achilles
Starting Member
3 Posts |
Posted - 2006-10-02 : 16:49:24
|
Thanks alot.... I got the desired result...but 'am confused with the logic here...can u plz explain it to me....-- GE start of this year hire_date >= dateadd(yy,@year-1900,0) and -- LT start of next year hire_date < dateadd(yy,@year-1899,0)this part of the code....quote: Originally posted by Michael Valentine Jones
select *from MyTablewhere -- GE start of this year hire_date >= dateadd(yy,@year-1900,0) and -- LT start of next year hire_date < dateadd(yy,@year-1899,0) CODO ERGO SUM
|
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
achilles
Starting Member
3 Posts |
Posted - 2006-10-03 : 09:57:36
|
Thank you so much micheal......it really helped me alot....I'll definitely go thru those links .....and try to understand as much as i can....Fianlly...thanks alot everybodyquote: Originally posted by Michael Valentine Jones Basically, it adds years to the datetime zero point, which is 1900-01-01. That is why it first subtracts 1900.You may want to read through these links:Converting Year, Month, and Day to DateTimeThe functions on this link will take input parameters of Year, Month, and Day and return a datetime.http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=22339This has a lot of background about SQL Server datetime.Date/Time Info and Script Linkshttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64762CODO ERGO SUM
|
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-03 : 10:20:25
|
Hereafter dont forget to Use DATETIME datatype to store DATEsMadhivananFailing to plan is Planning to fail |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2006-10-04 : 03:17:57
|
if you are using datetime fields could you not use USE PUBSdeclare @year as intset @year = 1993select * from employeewhere YEAR(hire_date) = @yearsteve-----------Don't worry head. The computer will do all the thinking from now on. |
|
|
|