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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Date field format

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 = day
mm = month
yy = year

hh = hour
mi = minute
weakday

Select datepart(dd, datecolumn) from yourtable

there is also datename, datediff, dateadd
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-04-05 : 14:24:41
SELECT RIGHT(CONVERT(varchar(7),GetDate(),111),2)+'/'+LEFT(CONVERT(varchar(7),GetDate(),111),4)


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

VPeters
Starting Member

20 Posts

Posted - 2007-04-05 : 14:40:03
Thank you Brett!!! This is EXACTLY what I needed!

quote:
Originally posted by X002548

SELECT RIGHT(CONVERT(varchar(7),GetDate(),111),2)+'/'+LEFT(CONVERT(varchar(7),GetDate(),111),4)


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam





Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-05 : 18:02:02
Somewhat shorter
SELECT RIGHT(CONVERT(VARCHAR, CURRENT_TIMESTAMP, 103), 7)

See this topic about available datetime formats
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=80563


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

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 there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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/2006
11/2007
12/2006
12/2007

when I suspect that you really want:

11/2006
12/2006
11/2007
12/2007






- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

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/2006
11/2007
12/2006
12/2007

when I suspect that you really want:

11/2006
12/2006
11/2007
12/2007






- Jeff
http://weblogs.sqlteam.com/JeffS


Go to Top of Page

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 yourtable

once 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
) x
group 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.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

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 yourtable

once 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
) x
group 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.

- Jeff
http://weblogs.sqlteam.com/JeffS


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-04-06 : 11:09:42
make sure you don't use my solution...it's too simple



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

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.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-04-06 : 11:55:50
quote:
Originally posted by VPeters

Thank you Brett!!! This is EXACTLY what I needed!



I really need to read the thread more closely



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

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 VPeters
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.



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!



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -