Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 help me to get date without dd

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: mar2007
so 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

Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2007-03-12 : 23:14:16
hi khtan datatype is datetime
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-12 : 23:17:51
you can use >= and <= or between

if @givendate >= @from_date
and @givendate <= @to_date
print 'yes'
else
print 'no'



KH

Go to Top of Page

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 checkdate

checkdate
-------------
march2007


this is fine.
if @givendate >= @from_date
and @givendate <= @to_date
print 'yes'
else
print 'no'

in this let us take an example if i give @givendate=result of the up ie march2007 and @fromdate=feb2006 ,@todate=apr2008
will it compare?is it possible to do thisif @givendate >= @from_date
and @givendate <= @to_date
print 'yes'
else
print 'no'lease let me know
Go to Top of Page

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 datetime

select @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

Go to Top of Page

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
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-13 : 00:58:00
no. It will not give you the correct result


KH

Go to Top of Page

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
Go to Top of Page

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

Go to Top of Page

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'
Go to Top of Page

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

Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2007-03-13 : 03:43:25
ok khtan thanks for you
Go to Top of Page
   

- Advertisement -