Author |
Topic |
ssunny
Posting Yak Master
133 Posts |
Posted - 2009-01-16 : 11:42:06
|
Hi All,I have a table with createdate column and datatype is smalldattime.createdate--------------2009-01-08 19:43:002009-01-14 11:03:002008-12-31 17:09:00 Now for a report I need to display createdate as12/31/20081/8/20091/14/2009.Now if I do select convert (varchar(25),'2009-01-08 19:43:00',110)it give me 2009-01-08 19:43:00similarly for select convert (varchar(25),'2009-01-08 19:43:00',101)I am getting 2009-01-08 19:43:00but I want 1/8/2009.Thanks in advance. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-16 : 12:07:25
|
whats the reporting tool you're using? if its sql reporting services, you just need to use report language as English (U.S) and then you can right click on cell displaying date and select format tab and chose datatype as date with above format (which is available as one of options) |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-01-16 : 12:08:32
|
select convert (varchar(25),cast('2009-01-08 19:43:00' as datetime),101) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-16 : 12:20:09
|
quote: Originally posted by rohitkumar select convert (varchar(25),cast('2009-01-08 19:43:00' as datetime),101)
its not advisable to change datatype of field to varchar just for formatting purpose. Formatting is a presentation issue which should be handled at you front end as long as you can. Its not purpose of Sql Server to do formatting of output. |
|
|
ssunny
Posting Yak Master
133 Posts |
Posted - 2009-01-16 : 12:26:17
|
Visakh, I am not using any tool for reporting. I am just using DTS package and generate report in .xls file.Rohit,your query worked for me but I am not getting result in the correct order.Here's my query:select count(customerid) as Total, convert (varchar(25),cast(createdate as datetime),101) as date from dbo.customer (nolock)where createdate > = '10/15/08' and createdate < = getdate()group by convert (varchar(25),cast(createdate as datetime),101)order by convert (varchar(25),cast(createdate as datetime),101) descresultI got:3 01/15/20099 11/03/20081 11/04/200816 11/05/20081 11/19/20081 11/26/20081 12/12/2008result I want:3 01/15/20091 12/12/20081 11/26/20081 11/19/200816 11/05/20081 11/04/20089 11/03/2008Thanks Guys. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-16 : 12:31:34
|
now i hope you understood problem of changing datatype . leaving it as datetime would have sorted it correctly. You can very well change format in excel by right clicking on column and selecting red format (there are various format available) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-16 : 12:33:11
|
[code]select count(customerid) as Total, dateadd(dd,datediff(dd,0,createdate),0) as [date] from dbo.customer (nolock)where createdate > = '10/15/08' and createdate < = getdate()group by dateadd(dd,datediff(dd,0,createdate),0)order by [date][/code] |
|
|
ssunny
Posting Yak Master
133 Posts |
Posted - 2009-01-16 : 12:45:31
|
quote: Originally posted by visakh16
select count(customerid) as Total, dateadd(dd,datediff(dd,0,createdate),0) as [date] from dbo.customer (nolock)where createdate > = '10/15/08' and createdate < = getdate()group by dateadd(dd,datediff(dd,0,createdate),0)order by [date]
Hey Visakh,This query works fine. And as you suggested I changed the format of date column in excel sheet and now report looks perfect.Today I learned something new and very important thing. Thanks so much.Thanks to Rohit also. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-16 : 12:55:11
|
You're welcome |
|
|
|