Why does using the left () method in getdate () change it to a different data type?

Running two simple select statements:

SELECT GETDATE() SELECT LEFT(GETDATE(), 10) 

Return:

 2015-10-30 14:19:56.697 Oct 30 201 

I was expecting LEFT() give me 2015-10-30 , but instead it is not.

Does anyone know why? Is this GETDATE data type type GETDATE ?

Thanks!

+7
sql sql-server getdate
source share
2 answers

GETDATE() returns a datetime value. When you execute SELECT GETDATE() , the application gets the datetime value and figure out how to display it. The application you use wisely chooses the ISO format.

When you execute LEFT(GETDATE() , the database must perform an implicit conversion from datetime to some string value. To do this, it uses its internationalization settings. What you see is based on these settings.

The moral of the story: Avoid implicit conversions. Always indicate what you are doing, especially in SQL, which has fairly weak diagnostic capabilities. So, use CONVERT() with the appropriate format for what you want to do.

+9
source share
Team

GETDATE () returns DATETIME , you want to return DATE

 SELECT CONVERT(DATE,GETDATE()); 
0
source share

All Articles