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)
 Date Formatting

Author  Topic 

madscientist
Starting Member

30 Posts

Posted - 2008-03-07 : 13:39:00
Hello everyone,

I have a date field called book_flag_date of varchar data type.

The values in the table look like this:

3/4/2008 14:32:59

OR

3/4/2008 14:9:0

The issue arises when I am ordering by. I want the value
3/4/2008 14:9:0 to format to 3/4/2008 14:09:00

Thank you very much for your help.

Will H
Yak Posting Veteran

56 Posts

Posted - 2008-03-07 : 15:53:05
First, read this:
http://weblogs.sqlteam.com/jeffs/archive/2007/04/13/format-date-sql-server.aspx

Then, Change the type from varchar to datetime. And then let your front-end app handle the format.
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-03-07 : 18:08:20
why, why, why do people store dates in varchar. I will never understand it.

in order to "format" the varchar field you would need to actuall ADD a character to it. Whereas if the field were stored properly as datetime, you could handle all the formatting on the front end, and would not have any sort issues.



Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2008-03-08 : 17:59:19
Heh... don't hold back... say what you really mean

--Jeff Moden
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-03-08 : 18:06:42
quote:
Originally posted by Jeff Moden

Heh... don't hold back... say what you really mean

--Jeff Moden



It is right up there creaing a column called "COUNT", populating it with 1's in Every row, then SUMming it, as a means of arriving at a count of records.



Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2008-03-08 : 20:29:26
LOL... isn't it easier to make an average that way? Don't have to worry about indexing it either ;-)

--Jeff Moden
Go to Top of Page

praveen_balanagendra
Starting Member

2 Posts

Posted - 2008-03-08 : 20:56:40
You can use the CONVERT function like this

SELECT CONVERT(DATETIME, '3/4/2008 14:9:0', 101) .. replace the date and timestamp with the actual table column name and use the order by statement for sorting.
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2008-03-08 : 21:38:30
Well done, Praveen... nice and simple. Let SQL do it for ya.

--Jeff Moden
Go to Top of Page
   

- Advertisement -