| Author |
Topic |
|
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2007-03-12 : 22:42:15
|
| 1.i need to be display date with out time ie as month and year ie like ex: mar2007so please help me to show date like this.2.if i have mar2007 as from date and apr2008 as to date.if i give dec2007 it lies between fromdate and todateso i want to check whether the date which given by me lies between from date and todate.so please any one help me achieve this two goals please |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-12 : 22:55:56
|
1. select datename(month, getdate()) + convert(varchar(4), datepart(year, getdate())) 2. What is the datatype of 'from date' and 'to date' ? KH |
 |
|
|
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2007-03-12 : 23:14:16
|
| hi khtan datatype is datetime |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-12 : 23:17:51
|
you can use >= and <= or betweenif @givendate >= @from_dateand @givendate <= @to_dateprint 'yes'elseprint 'no' KH |
 |
|
|
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2007-03-12 : 23:37:39
|
| hi khtan if i use select datename(month, getdate()) + convert(varchar(4), datepart(year, getdate())) as checkdatecheckdate-------------march2007this is fine.if @givendate >= @from_dateand @givendate <= @to_dateprint 'yes'elseprint 'no'in this let us take an example if i give @givendate=result of the up ie march2007 and @fromdate=feb2006 ,@todate=apr2008will it compare?is it possible to do thisif @givendate >= @from_dateand @givendate <= @to_dateprint 'yes'elseprint 'no'lease let me know |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-13 : 00:22:33
|
Maybe you can explain why you need the current date in string in the first place ?It will be very much easier if everything is in datetime data type.declare @today datetime, @fromdate datetime, @todate datetimeselect @today = getdate(), @fromdate = '20070301', @todate = '20071201'if @today >= @fromdate and @today <= @todate print 'YES'else print 'NO'if @today between @fromdate and @todate print 'YES'else print 'NO' KH |
 |
|
|
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2007-03-13 : 00:52:22
|
| hi khtan i am not using '20070301' format i am going to use march2007 as date.will it check>please let me know |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-13 : 00:58:00
|
no. It will not give you the correct result KH |
 |
|
|
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2007-03-13 : 02:18:18
|
| hi khtan i must use march2007 format only so please tell me how to compare |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-13 : 02:43:49
|
Basically can't. How can you compare string 'march2007' with 'december207' and determine which is earlier ?You will have to convert back to datetime to compare. Which means, convert march2007 back to '20070301' KH |
 |
|
|
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2007-03-13 : 02:50:24
|
| hi khtan can u please tell me how to convert march2007 to '20070301' |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-13 : 03:11:44
|
Why do you want to do that ?Why returning a date string, which makes comparison complicated ?quote: select datename(month, getdate()) + convert(varchar(4), datepart(year, getdate()))
why not just simple result in datetime rather than string ?select @somedate = dateadd(day, datediff(day, 0, getdate()), 0) And used it subsequently in your other query KH |
 |
|
|
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2007-03-13 : 03:43:25
|
| ok khtan thanks for you |
 |
|
|
|