Tuesday, January 16, 2018

How To Use Getdate() and Getutcdate() Functionality In Mssql

Hello Friend Today In This Blog I Will Show You How To Use Getdate() and Getutcdate() Functionality In Mssql.In SQL Server (Transact-SQL), the GETDATE function returns the current date and time.GETUTCDATE() represents the current UTC time (Universal Time Coordinate or Greenwich Mean Time).This SQL Server tutorial explains how to use the GETDATE function and GETUTCDATE function  in SQL Server (Transact-SQL) with syntax and examples.The GETDATE function returns the system date and time in the format 'yyyy-mm-dd hh:mi:ss.mmm'.The GETDATE is one of the most popular built-in methods of  Microsoft SQL Server, but unlike its name suggest, it doesn't return just date, instead it returns date with time information e.g. 2015-07-31 15:42:54.470 , quite similar to our own java.util.Date from Java world. If you want just date like 2015-07-31, or just time like 15:42:54.470 then you need to either CAST or CONVERT output of GETDATE function into DATE or TIME data type. From SQL Server 2008 onward, apart from DATETIME, which is used to store both date and time, You also have a DATE data type to store date without time e.g. 2015-07-31, and a TIME data type to store time without any date information e.g. 15:42:54.470. Since GETDATE() function return a DATETIME value, You have to use either CAST or CONVERT method to convert a DATETIME value to DATE or TIME in SQL Server.

If you are looking for a VARCHAR value, formatted into a specific format e.g. YYYYMMDD then you can also convert the DATETIME value to VARCHAR with a pattern using CONVERT function as shown in the last example of this article. I have also explained some difference between CAST and CONVERT methods and which one you should prefer.

Remember, DATE and TIME types are only available from Microsoft SQL Server version 2008 onward e.g. SQL Server 2012 or 2014, but you won't find them into older versions e.g. SQL Server 2004. See Microsoft SQL Server 2012 T-SQL Fundamentals to learn more about DATE, TIME and DATETIME data types.

In SQL Server (Transact-SQL), the GETUTCDATE function returns the current UTC date and time.There are no parameters or arguments for the GETUTCDATE function.The GETUTCDATE function returns the UTC date and time in the format 'yyyy-mm-dd hh:mi:ss.mmm'.The UTC date and time is determined by the operating system settings.GETUTCDATE returns Universal Time Constant (UTC) date and time as a timestamp. Because UTC time is the same everywhere on the planet, does not depend on the local timezone and is not subject to local time variants (such as Daylight Saving Time), this function is useful for applying consistent timestamps when users in different time zones access the same database.GETUTCDATE returns a timestamp in %TimeStamp data type format (yyyy-mm-dd hh:mm:ss.ffff).To change the default datetime string format, use the SET OPTION command with the various date and time options.Typical uses for GETUTCDATE are in the SELECT statement select list or in the WHERE clause of a query. In designing a report, GETUTCDATE can be used to print the current date and time each time the report is produced. GETUTCDATE is also useful for tracking activity, such as logging the time that a transaction occurred.GETUTCDATE can be used in CREATE TABLE to specify a field’s default value.


Syntax
The syntax for the GETDATE function in SQL Server (Transact-SQL) is:

GETDATE ( )


Syntax
The syntax for the GETUTCDATE function in SQL Server (Transact-SQL) is:

GETUTCDATE ( )



Here Is An Example Related To Topic

Step 1 :Create database

Create database User_db





Step 2:Create table

Here In This step We Will Create table.

Here In This Step We Required Four Column With Name Uids ,Uname ,Ucountry And UDateOfBirth using Parameter bigint,varchar and datetime.

create table User_table
(
Uids bigint,
Uname varchar(50),
Ucountry varchar(200),
UDateOfBirth datetime
);



Step 3 :Create Insert Trigger Functionality

Here In This Step We Will Insert Data Using Insert Command.
Data Will Be Added Manually Using Insert Command.



Insert Into User_table values(1,'VIRAJ','INDIA',Getdate())

Insert Into User_table values(2,'RAJ','USA',Getutcdate())

select *from User_table



Video Related To Topic Of this Blog Is Here....

HOW TO USE GETDATE FUNCTION AND TO SHOW DIFFERENT FORMAT OF DATETIME BY CONVERTING IT IN SQL QUERY 











No comments:

Post a Comment