?
Solved

Simple SQL Date Formula

Posted on 2005-03-19
7
Medium Priority
?
763 Views
Last Modified: 2012-08-13
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
Comment
Question by:dcass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 13582582
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
 

Author Comment

by:dcass
ID: 13582624
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
 

Author Comment

by:dcass
ID: 13582703
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 34

Expert Comment

by:Brian Crowe
ID: 13582710
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
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 13582717
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
 

Author Comment

by:dcass
ID: 13582739
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
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 1000 total points
ID: 13582777
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question