Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Formatting Dates with 3 Character Months (SQL Spackle) Expand / Collapse
Author
Message
Posted Thursday, January 20, 2011 3:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:08 AM
Points: 13,616, Visits: 10,501
katesl (1/20/2011)
Question from a DBA--

I have to ask this question because I have no experience doing user interface programming-- I use T-SQL and SSMS, and Excel to deliver reports, for everything I do. In the article, the T-SQL is presented in a window with horizontal scroll bars. What is the name by which to refer to such a window? It does not allow copy. Is this a problem of the particular browser I'm using or is it by design? With less elaborate user interface, I was able to copy the code from the article and paste it into the SSMS query window. Why is this scroll bar window supposed to be better than what worked just fine?

Thank you.


This is quite bizarre.
The first time I couldn't copy paste either from the article to test things out.
But when I opened the article for a second time, I could copy paste everything.

Weird IE problem?




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1050591
Posted Thursday, January 20, 2011 4:16 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 2:18 AM
Points: 1,180, Visits: 2,650
David McKinney (1/20/2011)
Très bien, Jeff! Except of course that you can't keep everybody happy!

In France, June and July are Juin and Juillet respectively, and hence JUI and JUI when truncated to 3 characters.


It gets even worse for languages such as Finnish, Estonian, & Czech (to name but a few).

For instance Czech uses the Roman numerals (I,II,III,IV etc.) for short month names, and in Estonian the short month names vary in length from 3 to 5 characters.



--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Post #1050626
Posted Thursday, January 20, 2011 4:25 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 5:03 AM
Points: 579, Visits: 2,520
Yes, the correct shortnames for the months are listed in sys.syslanguages

If you run this code, it should show you how, in SQL Server, all the languages format the current date using the date format code 113. You'll see that the shortmonthname is correctly inserted into the date if you use CONVERT. Is there a way of using DateName to get the correct short-date? I haven't discovered it!

DECLARE @TestEmOut NVARCHAR(MAX)
CREATE TABLE #ShortMonthNames (NAME NVARCHAR(20), Alias NVARCHAR(30), Date NVARCHAR(30), ShortMonthNames NVARCHAR(255))
SELECT @TestEmOut=''
SELECT @TestEmOut= @TestemOut+'
Set language N'''
+name+'''
insert into #shortMonthNames (Name, Alias, Date, ShortMonthNames)
SELECT N'''
+name+''',N'''+alias+''', CONVERT(nVARCHAR(80),GetDate(),113),
(Select shortmonths from sys.syslanguages where name like N'''
+NAME+''')'
FROM sys.syslanguages
EXEC(@TestEmOut)
SELECT * FROM #ShortMonthNames
DROP TABLE #ShortMonthNames


(Edited after David McKinney's help with setting the language to Brasilian portugese. Oops!)



Best wishes,

Phil Factor
Simple Talk
Post #1050628
Posted Thursday, January 20, 2011 4:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:27 AM
Points: 6,594, Visits: 8,878
surreydude. (1/20/2011)
One thing I did notice about the article, however, was the use of yy for year formats. It would seem that the "Y2K Bug" has not taught us very much afterall!


The only place where "yy" is being used in the article is in the DateName(yy, ) parts. In this, the "yy" (or "yyyy") is just the abbreviation for "year" - using any of these returns the full year (all four characters). See the DateName BOL - specifically look at the datepart chart in the Arguments section. And in the Return Value section is this:
Each datepart and its abbreviations return the same value.


So, would this be myth... busted?


Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1050634
Posted Thursday, January 20, 2011 5:41 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, August 25, 2014 5:29 AM
Points: 646, Visits: 1,862
WHERE name NOT IN ('Português (Brasil)'


Thanks Phil.

Putting the language in quotes will solve the Portuguese Brazil problem

Set language ['+name+']....
Post #1050671
Posted Thursday, January 20, 2011 5:53 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, August 21, 2014 5:29 AM
Points: 1,179, Visits: 786
Thanks Jeff for the article. i learned something about the datename function and these langauages settings.
Post #1050680
Posted Thursday, January 20, 2011 5:59 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, August 14, 2014 11:35 PM
Points: 316, Visits: 1,140
"You mean an 8% increase in performance with 44% less typing? Don't you wish you could do that everywhere?"

SOLD!


allmhuran.com - download the SSMSDeploy addin for SSMS 2008
Blog on sqlservercentral
Post #1050689
Posted Thursday, January 20, 2011 6:23 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, August 15, 2014 2:14 AM
Points: 15, Visits: 257
My experience are just opposite. DATENAME is slower than CONVERT + DATEPART. See below:


/*------------------------
DECLARE @BitBucket CHAR(4);

PRINT '========== DATENAME Method of Conversion ==========';
SET STATISTICS TIME ON;
SELECT @BitBucket = DATENAME(yy,SomeDate)
FROM dbo.JBMTest
SET STATISTICS TIME OFF;

PRINT '========== Original Method of Conversion CONVERT ==========';
SET STATISTICS TIME ON;
SELECT @BitBucket = CONVERT(CHAR(4), DATEPART(yy,SomeDate))
FROM dbo.JBMTest;
SET STATISTICS TIME OFF;

PRINT '========== Original Method of Conversion CAST ==========';
SET STATISTICS TIME ON;
SELECT @BitBucket = CAST(DATEPART(yy,SomeDate) AS CHAR(4))
FROM dbo.JBMTest;
SET STATISTICS TIME OFF;

------------------------*/
========== DATENAME Method of Conversion ==========

SQL Server Execution Times:
CPU time = 703 ms, elapsed time = 718 ms.
========== Original Method of Conversion CONVERT ==========

SQL Server Execution Times:
CPU time = 562 ms, elapsed time = 578 ms.
========== Original Method of Conversion CAST ==========

SQL Server Execution Times:
CPU time = 579 ms, elapsed time = 579 ms.

Post #1050710
Posted Thursday, January 20, 2011 6:25 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 15, 2014 7:11 AM
Points: 2, Visits: 395
This post is slightly off topic. I'm sure I stole this code from somewhere (probably from this site ), but I have it in my toolbox of SQL examples. It shows examples all of the CONVERT style codes. I added the abbreviated month + yyyy example from this Spackle post:

DECLARE @date DATETIME

SET @date = GETDATE()
--SET @date = '20090709T175449303'

;WITH cteDates (FormattedDate, Code, Style, SQL) AS
(
SELECT CONVERT(VARCHAR(35), @date, 0) AS FormattedDate,
'0' AS Code,
'Default' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 0)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 1) AS FormattedDate,
'1' AS Code,
'USA date - mm/dd/yy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 1)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 2) AS FormattedDate,
'2' AS Code,
'ANSI date - yy.mm.dd' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 2)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 3) AS FormattedDate,
'3' AS Code,
'UK/French date - dd/mm/yy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 3)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 4) AS FormattedDate,
'4' AS Code,
'German date - dd.mm.yy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 4)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 5) AS FormattedDate,
'5' AS Code,
'Italian date - dd-mm-yy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 5)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 6) AS FormattedDate,
'6' AS Code,
'Abbreviated month - dd mmm yy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 6)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 7) AS FormattedDate,
'7' AS Code,
'Abbreviated month - mmm dd, yy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 7)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 8) AS FormattedDate,
'8 OR 108' AS Code,
'24 hour time - hh:mm:ss' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 8)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 9) AS FormattedDate,
'9 OR 109' AS Code,
'Default with seconds and milliseconds appended mmm dd yyyy hh:mm:ss:mmmXM' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 9)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 10) AS FormattedDate,
'10' AS Code,
'USA date with hyphens - mm-dd-yy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 10)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 11) AS FormattedDate,
'11' AS Code,
'Japanese date - yy/mm/dd' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 11)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 12) AS FormattedDate,
'12' AS Code,
'ISO date - yymmdd' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 12)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 13) AS FormattedDate,
'13 OR 113' AS Code,
'European default with seconds and milliseconds - dd mon yyyy hh:mm:ss:mmm' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 13)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 14) AS FormattedDate,
'14 OR 114' AS Code,
'24 hour time with milliseconds - hh:mm:ss:mmm' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 14)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 20) AS FormattedDate,
'20 OR 120' AS Code,
'ODBC canonical date and time - yyyy-mm-dd hh:mm:ss' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 20)' AS SQL
UNION
SELECT SUBSTRING(CONVERT(VARCHAR(35), @date, 20), 1, 10) AS FormattedDate,
'20' AS Code,
'yyyy-mm-dd' AS Style,
'SELECT SUBSTRING(CONVERT(VARCHAR(35), @date, 20), 1, 10)' AS SQL
UNION
SELECT SUBSTRING(CONVERT(VARCHAR(35), @date, 20), 1, 16)
AS FormattedDate,
'20' AS Code,
'yyyy-mm-dd hh:mm' AS Style,
'SELECT SUBSTRING(CONVERT(VARCHAR(35), @date, 20), 1, 16)'
AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 21) AS FormattedDate,
'21 OR 121' AS Code,
'ODBC canonical date and time with milliseconds - yyyy-mm-dd hh:mm:ss.mmm' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 21)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 101) AS FormattedDate,
'101' AS Code,
'USA date with century - mm/dd/yyyy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 101)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 102) AS FormattedDate,
'102' AS Code,
'ANSI date with century - yyyy.mm.dd' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 102)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 103) AS FormattedDate,
'103' AS Code,
'UK / French date with century - dd/mm/yyyy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 103)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 104) AS FormattedDate,
'104' AS Code,
'German date with century - dd.mm.yyyy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 104)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 105) AS FormattedDate,
'105' AS Code,
'Italian date with century - dd-mm-yyyy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 105)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 106) AS FormattedDate,
'106' AS Code,
'Abbreviated month with century - dd mmm yyyy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 106)' AS SQL
UNION
SELECT RIGHT(CONVERT(CHAR(11),@date, 106), 8) AS FormattedDate,
'106' AS Code,
'Abbreviated month - mmm yyyy' AS Style,
'SELECT RIGHT(CONVERT(CHAR(11), @date, 106), 8)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 107) AS FormattedDate,
'107' AS Code,
'Abbreviated month with century - mmm dd, yyyy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 107)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 110) AS FormattedDate,
'110' AS Code,
'USA date with hyphens and century - mm-dd-yyyy' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 110)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 111) AS FormattedDate,
'111' AS Code,
'Japanese date with century - yyyy/mm/dd' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 111)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 112) AS FormattedDate,
'112' AS Code,
'ISO date with century - yyyymmdd' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 112)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 112) + ' ' + REPLACE(CONVERT(VARCHAR(35), @date, 108), ':', '')
AS FormattedDate,
'112 + 108' AS Code,
'yyyymmdd hhmmss' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 112) + '' '' + REPLACE(CONVERT(VARCHAR(35), @date, 108), '':'', '''')'
AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 112) + ' ' + REPLACE(CONVERT(VARCHAR(35), @date, 114), ':', '')
AS FormattedDate,
'112 + 114' AS Code,
'yyyymmdd hhmmssfff' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 112) + '' '' + REPLACE(CONVERT(VARCHAR(35), @date, 114), '':'', '''')'
AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 112) + ' ' + CONVERT(VARCHAR(35), @date, 114) AS FormattedDate,
'112 + 114' AS Code,
'yyyymmdd hh:mm:ss:fff' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 112) + '' '' + CONVERT(VARCHAR(35), @date, 114)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 126) AS FormattedDate,
'126' AS Code,
'ISO8601 - for use in XML - yyyy-mm-ddThh:mm:ss:mmm' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 126)' AS SQL
UNION
SELECT CONVERT(VARCHAR(35), @date, 127) AS FormattedDate,
'127' AS Code,
'ISO8601 with timezone Z - yyyy-mm-ddThh:mm:ss:mmmZ' AS Style,
'SELECT CONVERT(VARCHAR(35), @date, 127)' AS SQL
)
--
--SELECT * FROM cteDates AS cted ORDER BY cted.Code
SELECT SortKey =
CASE
WHEN CHARINDEX(' ', cted.Code) = 0
THEN cted.Code
ELSE
CAST(SUBSTRING(cted.Code, 1, CHARINDEX(' ', cted.Code)) AS INT)
END,
cted.FormattedDate, cted.Code, cted.Style, cted.SQL
FROM cteDates AS cted
ORDER BY SortKey, cted.FormattedDate

Post #1050713
Posted Thursday, January 20, 2011 6:28 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 5:03 AM
Points: 579, Visits: 2,520
Thanks to David for pointing out the bug in my code. I've gotten so used to the SET LANGUAGE syntax without the string delimiters! I've fixed the code in my original posting just in case anyone else uses it.


Best wishes,

Phil Factor
Simple Talk
Post #1050716
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse