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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Get todayand yesterday

Author  Topic 

zhshqzyc
Posting Yak Master

240 Posts

Posted - 2008-11-19 : 22:35:32
[code]declare @BeginDate varchar(50)
select @BeginDate = DATEADD(d, DATEDIFF(d, 0, GETDATE()) ,0)
select @BeginDate[/code]
I wnat to get
[code]yyyy-mm-dd[/code]
For ex.
2008-11-18
Please don't change the type of @BeginDate as timedate.I just want to keep it as varchar.

Thanks

cvraghu
Posting Yak Master

187 Posts

Posted - 2008-11-19 : 23:44:24
You can use CONVERT or CAST function to convert the datetime data to varchar in the specified format. Read BOL.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-20 : 01:22:04
and its not at all a good thing to store date values in varchar variables. that will make date manipulations complex. Try to preserve original data types as long as you can.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-20 : 02:09:00
<<
I just want to keep it as varchar.
>>

Why? Use proper DATETIME datatype to store dates

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-20 : 02:09:56
quote:
Originally posted by visakh16

and its not at all a good thing to store date values in varchar variables. that will make date manipulations complex. Try to preserve original data types as long as you can. and let the front end do the formation



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

zhshqzyc
Posting Yak Master

240 Posts

Posted - 2008-11-20 : 09:13:18
I need it as a varchar.
It will be concatenated with other strings.

Need help!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-20 : 09:22:58
[code]declare @BeginDate datetime
select @BeginDate = DATEADD(d, DATEDIFF(d, 0, GETDATE()) ,0)
select CONVERT(varchar(11),@BeginDate,121)
[/code]
Go to Top of Page

zhshqzyc
Posting Yak Master

240 Posts

Posted - 2008-11-20 : 10:00:10
Thanks.
If @BeginDate is a string, can we use
where t1 between @BeginDate and @EndDate

Does the system know it is representing time and date?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-20 : 10:15:33
quote:
Originally posted by zhshqzyc

Thanks.
If @BeginDate is a string, can we use
where t1 between @BeginDate and @EndDate

Does the system know it is representing time and date?


if t1 is datetime it will tried to convert it to datetime and if they contain valid date values it will work fine.
Go to Top of Page
   

- Advertisement -