Connecting Tech Pros Worldwide Forums | Help | Site Map

How to Format date without "Convert " function in T-SQL

Newbie
 
Join Date: May 2007
Location: Hyderabad
Posts: 13
#1: Jun 4 '07
I dont want to use Convert(Char(9),date,106) function to show date in dd/MM/yyyy format. It changes the datatype of my column to char & I cant perform other date operations on it without changing it back to datetime.
Is there any T-sql way of doing it?

iburyak's Avatar
Expert
 
Join Date: Nov 2006
Posts: 1,017
#2: Jun 4 '07

re: How to Format date without "Convert " function in T-SQL


Why would you care what format date has when you do data manipulation.
Use convert function only in select close when you try to display this date to a user.

Expand|Select|Wrap|Line Numbers
  1. SELECT Convert(Char(9),date,106) Date
  2. FROM some_table 
  3. WHERE date > dateadd(d, -10, getdate())
  4. ORDER BY date
Good Luck.
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#3: Jun 4 '07

re: How to Format date without "Convert " function in T-SQL


You are trying to get the datetime formatted in a non-standard format but still be able to use it in the date?

I would suggest creating a second column with the altered format, use that for viewing, and use the original for altering, updating, and calculating.
Newbie
 
Join Date: Dec 2006
Posts: 3
#4: Jun 12 '07

re: How to Format date without "Convert " function in T-SQL


A couple of other options:
A bit verbose, but you can do this:

select
dtForm = cast(day(dateField) as varchar) + '/' + cast(month(dateField) as varchar) + '/' + cast(year(dateField) as varchar)

obviously, "dateField" is the name of whatever date field you're pulling from the database. If you still wanted that as a date, you could always recast that back to datetime with the cast function, just like I was doing with the varchar.
Newbie
 
Join Date: Jun 2007
Posts: 2
#5: Jul 6 '07

re: How to Format date without "Convert " function in T-SQL


If you are doing it in a datagrid, just change the defaultcellstyle of the date column. "d" is the short date format mm/dd/yyyy

Me.dataGridView1.Columns("NameOfDateColumn").Defau ltCellStyle.Format = "d"

The cell will still hold the datetime value from the database, but will only show the converted date to the user.
Newbie
 
Join Date: Aug 2007
Posts: 3
#6: Aug 2 '07

re: How to Format date without "Convert " function in T-SQL


Quote:

Originally Posted by iburyak

Why would you care what format date has when you do data manipulation.
Use convert function only in select close when you try to display this date to a user.

Expand|Select|Wrap|Line Numbers
  1. SELECT Convert(Char(9),date,106) Date
  2. FROM some_table 
  3. WHERE date > dateadd(d, -10, getdate())
  4. ORDER BY date
Good Luck.


Excuse me, can you tell me why char(9) is used for an argument for Convert()
Thanks

Dave
Newbie
 
Join Date: Aug 2007
Posts: 3
#7: Apr 28 '08

re: How to Format date without "Convert " function in T-SQL


this link has the details explanation:
http://www.dreamincode.net/forums/showtopic42872.htm

manpakhong@hotmail.com


Quote:

Originally Posted by manpakhong

Excuse me, can you tell me why char(9) is used for an argument for Convert()
Thanks

Dave

Reply