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.
| Author |
Topic |
|
VPeters
Starting Member
20 Posts |
Posted - 2007-04-05 : 13:49:57
|
| Hi - Can anyone tell me how to remove the day and time from a datetime field, yet still keep the field functioning as a date for sorting/comparison purposes?? I was able to format the field as mm/yyyy, but it functions as a character string, and I am unable to convert it to a date format. |
|
|
rsegecin
Yak Posting Veteran
82 Posts |
Posted - 2007-04-05 : 14:02:40
|
| dd = daymm = monthyy = yearhh = hourmi = minuteweakdaySelect datepart(dd, datecolumn) from yourtablethere is also datename, datediff, dateadd |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
VPeters
Starting Member
20 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-04-05 : 23:30:05
|
quote: Originally posted by VPeters Hi - Can anyone tell me how to remove the day and time from a datetime field, yet still keep the field functioning as a date for sorting/comparison purposes?? I was able to format the field as mm/yyyy, but it functions as a character string, and I am unable to convert it to a date format.
Always my question is "Where do you want to show converted dates?"If you want them to show in front end application, use format function there. It is very easy to do all type of formations thereMadhivananFailing to plan is Planning to fail |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-04-06 : 10:01:16
|
| Keep in mind that none of the "solutions" given here return nothing but string values, not actual dates. You should work with date values in sql server and worry about formatting at the front end. To deal with months only, simply convert the datetime to the first day of the month or something like, and at your front-end you can easily format that any way you want. Now it compares and sorts as you expect. If you convert your dates to mm/yyyy string values, they will sort like this:11/200611/200712/200612/2007when I suspect that you really want:11/200612/200611/200712/2007- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
VPeters
Starting Member
20 Posts |
Posted - 2007-04-06 : 10:15:24
|
The problem is that this cannot be done on the front end and is not at all about formatting. I'll give you a brief overview of what I need to accomplish. I need to provide a monthly breakdown of customer orders. I also need to show which of the customers in a given month are repeat customers, by evaluating whether they had more than one order in the given month or had any orders PRIOR to the given month. The user is going to want to select the date range on the front end. It could be one month, 6 months, one year, etc. So I need to pull all data and do all my evaluations/groupings based on the month & year. I see that I won't be able to properly evaluate if the MM/YYYY in one record is less than the MM/YYYY in another record if it is a string, and that is definitely required. So if you have any suggestions, I'd appreciate it. Just keep in mind that all the coding needs to be done in the stored procedure. The dataset will contain a monthly breakdown of a count of customers, count of repeat customers, the dollars, and a few percentages based on those counts/dollars. Then the developer (I am not a developer) will take care of coding the report itself to make it use the data. Thanks!quote: Originally posted by jsmith8858 Keep in mind that none of the "solutions" given here return nothing but string values, not actual dates. You should work with date values in sql server and worry about formatting at the front end. To deal with months only, simply convert the datetime to the first day of the month or something like, and at your front-end you can easily format that any way you want. Now it compares and sorts as you expect. If you convert your dates to mm/yyyy string values, they will sort like this:11/200611/200712/200612/2007when I suspect that you really want:11/200612/200611/200712/2007- Jeffhttp://weblogs.sqlteam.com/JeffS
|
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-04-06 : 10:35:53
|
The solution is simple: Round the data to the nearest month, but keep it all as datetime's. This expression will take any date and return the 1st day of the month:select dateadd(month, datediff(month, 0, @date),0)so just wrap that expression around your dates like this:select dateadd(month, datediff(month, 0, YourDate),0) as [Month], ... other data ....from yourtableonce you verify for sure that the Month column is returning the first day of the month for your data, just wrap it in a derived table and group on it:select [Month], sum(...), sum(...) .. etc.from( select dateadd(month, datediff(month, 0, YourDate),0) as [Month], ... other data .... from yourtable) xgroup by [Month] and there you go. now, Month is an actual DateTime and always the first day of each month, it will sort and compare correctly, and if you group on it you get 1 row per month returned. And because it is still an actual datetime, it is easy to format it as "mm/yyyy" or whatever you want at the front-end.Hope this helps.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
VPeters
Starting Member
20 Posts |
Posted - 2007-04-06 : 11:08:17
|
You know, I got so off track with all of this date nonsense that I completely forgot I wanted to attempt to do just that - find the first of the month. Perfect. Thanks!quote: Originally posted by jsmith8858 The solution is simple: Round the data to the nearest month, but keep it all as datetime's. This expression will take any date and return the 1st day of the month:select dateadd(month, datediff(month, 0, @date),0)so just wrap that expression around your dates like this:select dateadd(month, datediff(month, 0, YourDate),0) as [Month], ... other data ....from yourtableonce you verify for sure that the Month column is returning the first day of the month for your data, just wrap it in a derived table and group on it:select [Month], sum(...), sum(...) .. etc.from( select dateadd(month, datediff(month, 0, YourDate),0) as [Month], ... other data .... from yourtable) xgroup by [Month] and there you go. now, Month is an actual DateTime and always the first day of each month, it will sort and compare correctly, and if you group on it you get 1 row per month returned. And because it is still an actual datetime, it is easy to format it as "mm/yyyy" or whatever you want at the front-end.Hope this helps.- Jeffhttp://weblogs.sqlteam.com/JeffS
|
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-04-06 : 11:48:30
|
| Brett -- did you read *any* of the rest of this thread ? Your solution is not only more complicated, but it doesn't do what he is looking for.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-04-06 : 12:07:58
|
quote: Originally posted by X002548
quote: Originally posted by VPeters Thank you Brett!!! This is EXACTLY what I needed!
I really need to read the thread more closely
Let me revise that for you:"I really need to read the ENTIRE thread more closely"from his first post:quote: Originally posted by VPetersI was able to format the field as mm/yyyy, but it functions as a character string, and I am unable to convert it to a date format.
from his last post:quote: Originally posted by VPeters You know, I got so off track with all of this date nonsense that I completely forgot I wanted to attempt to do just that - find the first of the month. Perfect. Thanks!
- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
|
|
|
|
|