Skip to main content

Posts

Showing posts with the label SQL insert from select query

5 Best Ways to Compare 2 DateTime - [SQL Server]

How to compare two datetime using SQL Server? This article introduces for  comparing two datetime in SQL Server and let’s explores in detail query examples as, => BASIC DATE COMPARISON --USING DATE FORMAT SELECT * FROM Customer WHERE CreatedOn > '2016-12-02' --USING DATETIME FORMAT SELECT * FROM Customer WHERE CreatedOn >= '2016-12-02:00:00:00' --USING DATE FORMAT SELECT * FROM Customer WHERE CreatedOn >= '2016-12-02' => OTHER WAYS TO COMPARE --USING DATE FORMAT SELECT * FROM Customer WHERE CreatedOn >= '2016-12-02' AND CreatedOn <= '2015-12-02' --USE OF BETWEEN OPERATOR. SELECT * FROM Customer WHERE CreatedOn BETWEEN '2016-12-02' AND '2015-12-02' I hope you are enjoying with this post! Please share with you friends!! Thank you!!!

SQL Server DateDiff - Date Format

The DATEDIFF function is use to get the differences in days, hours, minutes, seconds etc. and  It is returns the time between two dates. The DATEDIFF function can be used from above version of SQL Server 2005. --Syntax DATEDIFF(DATE_PART, START_DATE, END_DATE) --Types of, SELECT DATEDIFF( DAY , GETDATE(), GETDATE() + 1 ) AS DAY_DIFFERENCE SELECT DATEDIFF(WEEK, GETDATE(), GETDATE() + 1 ) AS WEEK_DIFFERENCE SELECT DATEDIFF(HOUR, GETDATE(), GETDATE() + 1 ) AS HOUR_DIFFERENCE SELECT DATEDIFF( MINUTE , GETDATE(), GETDATE() + 1 ) AS MINUTE_DIFFERENCE SELECT DATEDIFF( SECOND , GETDATE(), GETDATE() + 1 ) AS SECOND_DIFFERENCE -- Example as, SELECT DATEDIFF( YEAR , '2015/04/28' , '2016/04/28' ); --RESULT is: 1 SELECT DATEDIFF(YYYY, '2015/04/28' , '2016/04/28' ); --RESULT is: 1 SELECT DATEDIFF(YY, '2015/04/28' , '2016/04/28' ); --RESULT is: 1 SELECT DATEDIFF( MONTH , '2015/01/01' , '2016/04...

What is @@IDENTITY in SQL? When we should use and scope of @@IDENTITY?

What is @@IDENTITY in SQL? The @@IDENTITY is a system function which returns the last inserted identity value. All the @@IDENTITY, SCOPE_IDENTITY and IDENT_CURRENT are similar functions because all are return the last inserted value into the table’s IDENTITY columns. The @@IDENTITY and SCOPE_IDENTITY return the current session last identity value but the SCOPE_IDENTITY returns the current scope value. What is the scope of @@IDENTITY? In the @@IDENTITY, there are no any limitations for a specific scope. Syntax : - @@IDENTITY    Return Type : - numeric ( 38 , 0 ) For example as, -- USE OF @@IDENTITY INSERT INTO ContactType(Code, Description, IsCurrent, CreatedBy, CreatedOn) VALUES ( 'IT-PROGRAMING' , 'This is a Prrogrammer!' , 1 , 'Anil Singh' , GETDATE()); GO SELECT @@ IDENTITY AS 'COL_IDENTITY' ; GO The Use of  SCOPE_IDENTITY :- -- DECLARE RETURN TABLE DECLARE @Return_Table TABLE (Cod...

SQL Server INSERT from SELECT query

There are following ways to INSERT and SELECT statements in the table. 1.        You can use the INSERT statement to specify values directly. 2.        You can use the INSERT statement to specify values from a  sub-query . 3.        You can use the SELECT statement with the INTO clause. Syntax : INSERT [INTO] Table_Name ( COLUMNS ) Table_Values Example: You can use the below query for insert rows. INSERT INTO [dbo] . Users ( ID , Name , DepName )    SELECT ID , Name , DepName                 FROM [dbo] . OldUsers If you have same columns in the both tables (destination and source) that time you can use below query for insert rows . INSERT INTO [dbo] . Users   SELECT *   FROM dbo . Customers I think, It might help you. Thank you! ...