MySQL – Functions to get current date and time

You may often need to do some data filters based on the current date and time. In MySQL you can use the system functions available to get current date and time.

Refer the following examples.

1 Get current date

SELECT CURRENT_DATE() AS today_date

or

SELECT CURDATE() AS today_date

The result is

2017-11-20

2 Get current time

SELECT CURRENT_TIME() AS today_time

or

SELECT CURTIME() AS today_time

The result is

08:36:29

3 Get Current Date and time

SELECT NOW() AS today_date_time

The result is

2017-11-20T08:36:29Z

MySQL – Simple ways to know version number with @@version and version()

If you want to know the version number of MySQL server, there are two simple methods available.

You can use system variable @@VERSION as shown below

SELECT @@VERSION as mysql_version;

Other method is to use system function VERSION()

SELECT VERSION() as mysql_version

Both of them returns the following result

mysql_version
-----------------
5.6.35

Depends on the version of the server, you may get the different version number

Note : @@VERSION is already available in SQL Server database engine as a global variable to get the version information

Alias name should be preceded by “AS”

In my earlier post on Understanding Column alias I have shown different methods of using a column alias name

SELECT 100number
SELECT 100.number
SELECT 100'number'
SELECT 100"number"
SELECT 100[number]

As you see all works fine with number as the column alias name

But However you need to use AS before the alias name for better reading and standard
Now execute the following and see the result

drop table if exists #employee_master;
create table #employee_master
(
emp_id int, emp_name varchar(100), dob datetime
)
insert into #employee_master
select 1,'Sankar','19760910' union all
select 2,'Kamal','19680707' union all
select 3,'Rajesh','19661222'
Select emp_id, emp_name dob from #employee_master

The result is

emp_id      dob
----------- ------------
1           Sankar
2           Kamal
3           Rajesh

The SELECT statement lists out three columns but the output has only two columns. The data of emp_name are listed under the column name dob. It is because the missing comma between column names makes that dob becomes alias name for emp_name which is confusing and hard to detect if there are hundreds of lines in the procedure.

So in my opinion ,Alias name should be preceded by “AS” to avoid confusion and unexpected result.

What do you think of this?

Adding time values stored as numbers

One of my friends told me that he had a data set in which numbers are expressed in decimal format.

For example 3.45 means 3 hours and 45 minutes, 4.30 means 4 hours 30 minutes etc.

The challenge is to add these numbers not as decimal values but as time values so adding 3.45 and 4.30 should be 8.15 and not 7.75.

Let us create this data set

create table #t (gtime numeric(8,2)) 
insert into #t values (3.45),(4.30),(4.45),(4.45) 

There are two methods that I come up with

Method 1 : Use CONVERT function

select 
convert(varchar(10), 
	dateadd(minute,sum(floor(gtime)*60+gtime%1*100),0),
108)  
        as time_value
from 
	#t   

The result is

time_value
----------
17:45:00

Method 2 : CONVERT the sum into TIME

select 
	cast(dateadd(minute,sum(floor(gtime)*60+gtime%1*100),0) as time)  
        as time_value
from 
	#t 

The result is

time_value
----------------
17:45:00.0000000