Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


using the format() command


using the format() command

Author
Message
Tobar
Tobar
SSC Veteran
SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)

Group: General Forum Members
Points: 267 Visits: 758
Anyone point me to the documentation, or explain why you would expect these results?
DECLARE @d DATETIME = '07/25/2013 10:11:12.345';
SELECT FORMAT ( @d, 'yyyy dd mm hh mm ss')

2013 25 11 10 11 12


(I am passing it the modifier "mm" twice. The first time it is interpretted as month the second as minute.)

<><
Livin' down on the cube farm. Left, left, then a right.
Alan.B
Alan.B
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2242 Visits: 7426
Tobar (7/25/2013)
Anyone point me to the documentation, or explain why you would expect these results?
DECLARE @d DATETIME = '07/25/2013 10:11:12.345';
SELECT FORMAT ( @d, 'yyyy dd mm hh mm ss')

2013 25 11 10 11 12


(I am passing it the modifier "mm" twice. The first time it is interpretted as month the second as minute.)


There's not a lot out there on FORMAT. There's this: http://msdn.microsoft.com/en-us/library/hh213505.aspx. It's not very good IMHO but that's what Microsoft has out there. If you have Microsoft SQL Server 2012 T-SQL Fundamentals (Itzek Ben Gan) he mentions it briefly and refers you to: http://msdn.microsoft.com/library/26etazsy.aspx.

Anyhow, I didn't completely get what you are asking here but... MM=month; mm=minute

This:

DECLARE @d DATETIME = '07/25/2013 10:11:12.345';
SELECT FORMAT ( @d, 'yyyy dd MM hh mm ss')



...will get you this:

2013 25 07 10 11 12



This is also a good article about FORMAT: http://sqlsafety.blogspot.com/2012/11/t-sql-format-built-in-function.html


EDIT: Typo, added link.

-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
Tobar
Tobar
SSC Veteran
SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)

Group: General Forum Members
Points: 267 Visits: 758
Thanks Alan. If nothing else it is nice to hear that "there isn't a lot out there" from someone else.

<><
Livin' down on the cube farm. Left, left, then a right.
Tobar
Tobar
SSC Veteran
SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)SSC Veteran (267 reputation)

Group: General Forum Members
Points: 267 Visits: 758
Sorry Alan, didn't read your response close enough first time.

Alan.B (7/25/2013)
Tobar (7/25/2013)
Anyone point me to the documentation, or explain why you would expect these results?
DECLARE @d DATETIME = '07/25/2013 10:11:12.345';
SELECT FORMAT ( @d, 'yyyy dd mm hh mm ss')

2013 25 11 10 11 12


(I am passing it the modifier "mm" twice. The first time it is interpretted as month the second as minute.)


Anyhow, I didn't completely get what you are asking here but... MM=month; mm=minute

This:

DECLARE @d DATETIME = '07/25/2013 10:11:12.345';
SELECT FORMAT ( @d, 'yyyy dd MM hh mm ss')



...will get you this:

2013 25 07 10 11 12



That is just it. I us mm twice in my string. The first time it interprets it as MM (supposedly "hour") the second time as mm (supposedly minute).

<><
Livin' down on the cube farm. Left, left, then a right.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search