473,394 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

13
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?
Jun 4 '07 #1
6 24296
iburyak
1,017 Expert 512MB
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.
Jun 4 '07 #2
Motoma
3,237 Expert 2GB
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.
Jun 4 '07 #3
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.
Jun 12 '07 #4
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.
Jul 6 '07 #5
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
Aug 2 '07 #6
this link has the details explanation:
http://www.dreamincode.net/forums/showtopic42872.htm

manpakhong@hotmail.com


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

Dave
Apr 28 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Westcoast Sheri | last post by:
When I used the "date("nj")" function to generate an invoice, about 1 out of 500 times, nothing comes up <?php $invoicedate = date("nj"); ?> <html><head></head><body> Invoice date: <?php...
4
by: Funnyweb | last post by:
I have just notices that the date() function is not returning the correct date/time on my "server". I am running apache2 on my winxp pro laptop. My system clock is set to the correct date,...
2
by: ethoemmes | last post by:
Hi I am trying to use the below functions to return the month for a given date. I want the month displayed as Jan for 01 so I wrapped the Month function in the Format function. Month:...
4
by: Patrick McGuire | last post by:
I need to get the current date in vb .NET. In vba I always used the Date() function, and the help in .NET indicates that this function still exists, but I can't seem to find it. The Now()...
10
by: KJ | last post by:
Hello, Why does this not work? txtMoney.Text.Format("#,##0.00") If I type 100 in txtMoney, it returns #,##0.00 I have never gotten the format function to work in .NET.
16
by: Al Reid | last post by:
First, I'm using vb2005. I have a string that is read from a barcode reader into a TextBox. The string is 6 characters long and represents a date (mmddyy). I want to display it to the user in a...
4
by: Steve Long | last post by:
Hello, I'm encountering a bug, or user error, with the Format function and I'm wondering if someone can either straighten me out or confirm the bug. I'm passing in a Date to the Format function...
2
by: fniles | last post by:
How do you do Format(Date, "mmddyy") in VB.NET ? Thank you
11
by: walterbyrd | last post by:
My MySQL table has a field that is set as type "date." I need to get today's date, and insert it into that field. The default for that MySQL field is 2006-00-00. I know about the date()...
3
by: murch.alexander | last post by:
I made a simple public function to set and return a date value (see below). I have a number of queries that call up the function to get the "As Of Date," which is typically set to today's date....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.