Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does using left() on getdate() change it to a different data type?

Running two simple select statements:

SELECT GETDATE() 

SELECT LEFT(GETDATE(), 10)

Returns:

2015-10-30 14:19:56.697 

Oct 30 201

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

Does anyone know why? Is it to do with the style of the data type GETDATE returns?

Thanks!

like image 525
S.Beeley Avatar asked Oct 30 '15 14:10

S.Beeley


People also ask

What data type does Getdate () return?

Description. GETDATE returns the current local date and time for this timezone as a timestamp; it adjusts for local time variants, such as Daylight Saving Time. GETDATE can return a timestamp in either %TimeStamp data type format (yyyy-mm-dd hh:mm:ss.

What is equivalent of Getdate () in MySQL?

The Oracle and MySQL equivalent of GETDATE is SYSDATE.

How do I change the Getdate in SQL Server?

CREATE OR ALTER FUNCTION [dbo]. GetDate RETURNS datetime2 AS BEGIN RETURN ISNULL((SELECT CurrentMoment FROM dbo. System), SYSDATETIME()); END GO -- Yes the above returns a more accurate clock than GETDATE(). Replace ALL references to GETDATE() with dbo.


1 Answers

GETDATE() returns a datetime value. When you do SELECT GETDATE(), then the application is getting a datetime value and figuring out how to display it. The application you are using is wisely choosing an ISO-standard format.

When you do LEFT(GETDATE(), then the database needs to do an implicit conversion from datetime to some string value. For this, it uses its internationalization settings. What you are seeing is based on these settings.

Moral of the story: avoid implicit conversions. Always be explicit about what you are doing, particularly in SQL which has rather poor diagnostic capabilities. So, use CONVERT() with the appropriate format for what you want to do.

like image 191
Gordon Linoff Avatar answered Oct 04 '22 16:10

Gordon Linoff