Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

SQL SERVER Puzzle Change in Date Format with Function

$
0
0

Last week we had amazing time with an interesting puzzle about Datetime. You can see the puzzle over SQL SERVER Puzzle Playing with Datetime with Customer Data .The puzzle was extremely well received and lots of people asked me to bring another puzzle which can help us learn something new. Looking at the request of everyone, here is another very simple puzzle with Date Format.


SQL SERVER   Puzzle   Change in Date Format with Function

This puzzle is very simple first, execute following script.

SELECT GETDATE()

When you execute above script, you will notice that it will display the current date in the format as yyyy-mm-dd, along with the date it is also displayingtime values as well.

Resultset: 2016-09-19 12:32:58.737

Now let us assume that we do not want the time partin our display, we only want to display date part of the date time value. There are many different methods to get only date part, here is a relevant blog post: SQL SERVER Retrieve Select Only Date Part From DateTime Best Practice .

Now let us assume that we have not read the blog post about how to select only date part from datetime. Now from visual inspection, it is very clear that we only want to retrieve left 11 characters from the datetime value. In SQL Server we have function LEFT(string,n) when we apply it over any string, it returns us ncharacters starting from the left. Let us apply the LEFT () function over our datetime function and retrieveonly date part from it.

SELECT LEFT(GETDATE(),11)

When you execute above script, it does give us only date part, but the value of the data part is changed fromyyyy-mm-dd to mmm dd yyyy.

Resultset: Sep 19 2016

The Puzzling question is

Why didWhy did displayed date format changed from yyyy-mm-dd to mmm dd yyyy?


SQL SERVER   Puzzle   Change in Date Format with Function

Well, just leave a comment in the blog post and after 2 days I will publish all the comments. If you know the answer of this puzzle, I suggest you ask this question to your friends and see if they know the answer of the same.

Reference: Pinal Dave ( http://blog.sqlauthority.com )


Viewing all articles
Browse latest Browse all 3160

Trending Articles