Pages

Monday, June 2, 2014

SQL Function - Now( )

The Now ( ) Function returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format, based where the function is used, whether the function is used in a string or numeric context. The value is expressed in the current time zone of the server where the database has nested.

SQL Function NOW( ) Syntax:

SELECT NOW() FROM table_name;

SQL Function NOW( ) Example:


Assumed that we have a table named “Products” given below.

ProductID
ProductName
Price
1
Ipoh Coffee 
46
2
Gula Malacca 
19.45
3
Røgede sild 
9.5
4
Spegesild 
12
5
Zaanse koeken 
9.5
6
Chocolade 
12.75

We are going to select Product Name and Price as this is current price of the product.

The syntax for Format:

SELECT ProductName, Price, Now() AS PriceDateTime
FROM Products;

Output:
ProductName
Price
PriceDate
Ipoh Coffee 
46 
2014-05-29 1:14:34 AM 
Gula Malacca 
19.45 
2014-05-29 1:14:34 AM 
Røgede sild 
9.5 
2014-05-29 1:14:34 AM
Spegesild 
12 
2014-05-29 1:14:34 AM
Zaanse koeken 
9.5 
2014-05-29 1:14:34 AM
Chocolade 
12.75 
2014-05-29 1:14:34 AM


Similarly you can use Time( ) and Date ( ) function.

No comments:

Post a Comment