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 |
pmidge81
Starting Member
2 Posts |
Posted - 2010-01-26 : 09:59:22
|
Hello.I'm fairly new to using sql, so I could do with a bit of help.I'm using the convert function to pull a date which is 60 days from the current date. My problem is, I want the format to output as, for example : 27th March 2010, with the full month written out, as well as 'th' or 'st' after the day.At the moment I'm using : SELECT CONVERT(VarChar(20), DATEADD(DD, 60, GETDATE()), 106) - which is giving me 27 Mar 2010. I can't seem to find a style which gives me the format I want. Does anyone know of a way I can do this?Thanks in advance for any help. |
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2010-01-26 : 10:18:36
|
That doesn't come as a "standard" CONVERT output , so you'll need to create a function that resturns the relevant suffix.One approach is , initially you could extract the individdual elements , such as : SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) + ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [DD Month YYYY]and then build a function , which would have an input parameter : i.e CAST(DAY(GETDATE()) AS VARCHAR(2)) . The function would see which day it is and return the relevant suffixJack Vamvas--------------------http://www.ITjobfeed.com |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-26 : 10:19:38
|
The normal advice is to format the data in your application, SQL isn't really the place to do it (it causes conversion of your Date Datatype to String, and then everything thereafter works assuming its a string - can't sort chronologically, can't compare, etc. thus formatting normally the last step before "presenting" to the user.There isn't a format "27th March 2010", you'd have to do with with messy Find & Replace type string handling |
|
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2010-01-26 : 10:25:52
|
I agree that should be done at the presentation layer - but this depends where the application deals with conversions of datetime into a string format for presentation. But certainly the logic used above - could be applied in an e.g asp page - using most scripting languagesJack Vamvas--------------------http://www.ITjobfeed.com |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-26 : 10:35:40
|
Yup, age-old problem - application can just take the data straight from SQL and shove it out to the user, or it can fiddle with it first to make ti look pretty We have Applications stuff that shoves the data straight out the door to the user (where we want quick & dirty thrown together mini-app). In that instance we use "hints" in the column name:SELECT [_DATE_Invoice Date] = InvoiceDate, [_RHJ_Amount] = InvoiceAmountFROM MyInvoiceTable... so that the APP does default date formatting, and right-hand-justification etc. and strips the "hint" off the column name - all without the DEVs having to knock some code together. |
|
|
pmidge81
Starting Member
2 Posts |
Posted - 2010-01-26 : 11:22:29
|
Thanks for the replies. Seems the easy solution that I was hoping for isn't possible! I am sending out emails on behalf of a client, which contain an "offer ends" field, that will go out automatically each day, and need to have that days date plus 60 in it. It will use the same query each day, so I can always manually change the query each day and insert the correct date myself, but I was hoping for a simple way to automate this so I wouldn't have to do it that way. It was just my laziness coming to the forefront!Thanks again for your help |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-26 : 11:33:24
|
"It was just my laziness coming to the forefront!"Nothing wrong with that!Can the thing that sends the Email convert a datetime datatype into a "pretty date"?If not it would be worth messing around in SQL making the "pretty date". but the SQL code won't be pretty! |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
|
|
|
|