Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 764
  • Last Modified:

Simple SQL Date Formula

I have searched for literally five hours and cannot find such a simple thing.
I am creating an SQL database and want to put in a column formula for both a date and a time field (both defined datetime).
I need one to be the date in yyyy/mm/dd format and one to be mm/dd/yyyy hh/mm format.
I've tried putting in the above (I know that minutes are nn) "yyyy/mm/dd" & "mm/dd/yyyy hh/nn"  respectively but it gives the error "Error validating the formula for column 'TDate'".
I feel like this is the most ridiculous qustion I've ever asked.
0
dcass
Asked:
dcass
  • 4
  • 3
1 Solution
 
Brian CroweDatabase AdministratorCommented:
The reason you can't find it is because it doesn't exist.  You are confusing format with data.  The database stores the data... you can pull it into a format if you like but the best way to do this is in an application not in the DB.  SQL does not have a date-only or a time-only datatype.  A datetime is stored as 2 4-byte integer values with one representing the number of days since 1/1/1900 and the other representing the number of milliseconds (more or less) since midnight.  If you give a more detailed example of what you are trying to accomplish then I can get more precise.
0
 
dcassAuthor Commented:
I know SQL doesn't have a date-only or time-only datatype, but you can put it in different formats by setiing the column format, can't you?  It doesn't have to be in mm/dd/yyyy - I know you can change it in the code, I was just looking for an automatic switch from mm/dd/yyyy to yyyy/mm/dd and thought I could use column format.
0
 
dcassAuthor Commented:
If you can't do it that way, how do you switch the date in C# from mm/dd/yyyy to yyyy/mm/dd?
Should I store the first field that I want just the date in as a varchar?  How do I strip off the time?
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
Brian CroweDatabase AdministratorCommented:
It's stored the same no matter how you put it in as long as it is a valid datetime.  If you want to display it in different formats then you can use the Convert function

SELECT Convert(varchar, TDate, 101) --for mm/dd/yyyy
            Convert(varchar, TDate, 111) --for yy/mm/dd

Check out Convert in BOL for different formats
0
 
Brian CroweDatabase AdministratorCommented:
I'm not sure about C# but in VB.Net i would do something like

myDate.tostring("mm/dd/yyyy") if myDate is a datetime or
CDate(myDateString).tostring("mm/dd/yyyy") if i'm dealing with a date in string form.
0
 
dcassAuthor Commented:
Also, I want to run a 24 hour clock and not use am/pm.  Is it possible to store it that way so it sorts correctly?
0
 
Brian CroweDatabase AdministratorCommented:
You're still confusing storage value and format.  I'm sure that in C# you can create a custom format just like you can in VB.Net to apply to the control where you want to format the value.

dim strFormat1 as string = "yyyy/mm/dd HH:mm"
dim strFormat2 as string = "mm/dd/yyyy"

textbox1.text = CDate(myDatatable.rows(0).item("TDate")).tostring(strFormat1)
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now