SlideShare uma empresa Scribd logo
1 de 20
Top 5 Built-in Functions
    (Transact-SQL)




               Manas Ranjan Dash




                              @simplymanas
Agenda



Programmability Enhancements (Database Engine)

select (my) top 5 * from SQL_BuiltIn_Functions




  **All the function described in this session are purely related to SQL2012.
  There is no resemblance with SQL2005 or SQL2008. Compatibility level 110
What is today’s date?
What is tomorrow’s date?



    SELECT GETDATE()

   SELECT GETDATE()+1
Beginning of the month?



SELECT CAST (
CAST (YEAR(GETDATE()) AS VARCHAR) + '-'+
CAST(MONTH(GETDATE()) AS VARCHAR) + '-'+
 '01' AS DATE)
Beginning of the month?

    I am from
     SQL 2012

 SELECT CAST(
 CONCAT(
 YEAR(GETDATE()),'-',
 MONTH(GETDATE()),'-',
 '01')
 AS DATE)
Display Full Name

     SQL
   SERVER
    2012
SELECT
SELECT
CONCAT(
  FIRSTNAME +' ' +
  ISNULL(FIRSTNAME,'') +' ' +
  FIRSTNAME,' ',
  MIDDLENAME + ' ' +
  ISNULL(MIDDLENAME,'') + ' ' +
  MIDDLENAME,' ',
  LASTNAME AS FULLNAME AS FULLNAME
  ISNULL(LASTTNAME,'')
  LASTNAME
FROM PERSON.PERSON
  ) AS FULLNAME
FROM PERSON.PERSON
CONCAT() Function



                    BOL
 CONCAT ( string_value1, string_value2 [, string_valueN ] )
 All arguments are implicitly converted to string types and then
   concatenated.
                  BACHCHAN
 Null values are implicitly converted to an empty string.
 If all the arguments are null, an empty string of type varchar(1)
   is returned.


                                                 TOP 1
Beginning of the month?

            SQL
SELECT CAST (
          SERVER
           2012
  CONCAT
SELECT
  (
  DATEFROMPARTS (
     YEAR(GETDATE()),'-',
     YEAR(GETDATE()),
     MONTH(GETDATE()),'-',
     MONTH(GETDATE()),
     '01'
     1)
  )
AS DATE)
DATEFROMPARTS() Function




                    BOL
 DATEFROMPARTS ( year, month, day )
 DATEFROMPARTS returns a date value with the date portion

                BACHCHAN
   set to the specified year, month and day.




                                               TOP 2
End of the month?

  SELECT DATEADD
      SQL
SELECT DATEADD
       (D,-1,
    SERVER
   (D,-1, DATEADD
     2012 DATEFROMPARTS(
   SELECT
          CASE WHEN MONTH(GETDATE())<12
   DATEFROMPARTS(
   (D,-1, YEAR(GETDATE())
          THEN DECEMBER
   IIF(MONTH(GETDATE())<12,
   DATEFROMPARTS(YEAR(GETDATE()),
          ELSE YEAR(GETDATE())+1
        YEAR(GETDATE()),YEAR(GETDATE()+1)),
          END,
   MONTH(GETDATE())+1,1) MONTH(GETDATE())+1),
   IIF(MONTH(GETDATE())=12,1,
          CASE WHEN MONTH(GETDATE())=12 THEN 1
   )
   1)     ELSE MONTH(GETDATE())+1
)         END,
       1)
  )
IIF() Function




                     BOL
 IIF ( boolean_expression, true_value, false_value)
 IIF is translated into CASE

                 BACHCHAN
 IIF statements can also be nested only up to the maximum
   level of 10 like CASE.




                                               TOP 3
End of the month?

             SQL
--THIS MONTH SERVER
              END
SELECT DATEADD
SELECT EOMONTH(GETDATE())
              2012
   (D,-1,
--NEXT MONTH END
   DATEFROMPARTS(
SELECT EOMONTH(GETDATE(),1)
   IIF(MONTH(GETDATE())<12,
SELECT EOMONTH(GETDATE(),2)
      YEAR(GETDATE()),YEAR(GETDATE()+1)),
SELECT EOMONTH(GETDATE(),14)
   IIF(MONTH(GETDATE())=12,1, MONTH(GETDATE())+1),
--PREVIOUS MONTH END
   1)
SELECT EOMONTH(GETDATE(),-1)
)
SELECT EOMONTH(GETDATE(),-2)
SELECT EOMONTH(GETDATE(),-14)
EOMONTH() Function




                    BOL
 EOMONTH ( start_date [, month_to_add ] )
 Even if you pass a string parameter you will get a date return
  type as it does a implicit conversion.


                BACHCHAN
DECLARE @date VARCHAR(255)
SET @date = '12/1/2011'
SELECT EOMONTH ( @date ) AS Result
GO



                                                TOP 4
Lets Format a Date


  SELECT   CONVERT(VARCHAR,   GETDATE(),   101)   --11/23/2012
  SELECT   CONVERT(VARCHAR,   GETDATE(),   102)   --2012.11.23
  SELECT   CONVERT(VARCHAR,   GETDATE(),   103)   --23/11/2012
              SQL
  SELECT   CONVERT(VARCHAR,   GETDATE(),   104)   --23.11.2012
            SERVER
  SELECT   CONVERT(VARCHAR,
             2012
                              GETDATE(),   105)   --23-11-2012
  SELECT   CONVERT(VARCHAR,   GETDATE(),   108)   --10:04:52

SELECT FORMAT( GETDATE(), 'yyyy-MM-dd HH:MM') --2012-11-23 10:11
SELECT FORMAT( GETDATE(),' "First SQLUG Meet On" ddd dd"th" MMMM
yyyy "at" HH:MM ')
-- First SQLUG Meet On Fri 24th November 2012 at 10:11
FORMAT() Function




                   BOL
 FORMAT ( value, format [, culture ] )


                BACHCHAN
 Use the FORMAT function for locale-aware formatting of
  date/time and number values as strings. For general data type
  conversions, use CAST or CONVERT.




                                              TOP 5
CULTURE


 You can use the SET LANGUAGE statement.
 Culture accepts any culture supported by the .NET Framework
  as an argument;
 it is not limited to the languages explicitly supported by SQL
  Server

DECLARE @d DATETIME   = '01/01/2011';
SELECT FORMAT ( @d,   'd', 'en-US' ) AS   'US English Result'
      ,FORMAT ( @d,   'd', 'en-gb' ) AS   'Great Britain English Result'
      ,FORMAT ( @d,   'd', 'de-de' ) AS   'German Result'
      ,FORMAT ( @d,   'd', 'zh-cn' ) AS   'Simplified Chinese (PRC) Result';
NEWLY ADDED FUNCTIONS


Conversion functions
PARSE ()
TRY_CONVERT ()
TRY_PARSE ()
Date and time functions
DATEFROMPARTS ()
DATETIME2FROMPARTS ()        Logical functions
DATETIMEFROMPARTS ()         CHOOSE ()
DATETIMEOFFSETFROMPARTS ()   IIF ()
                                                 The existing LOG (Transact-
EOMONTH ()                   String functions    SQL) function now has an
SMALLDATETIMEFROMPARTS ()    CONCAT ()           optional second base parameter.
TIMEFROMPARTS ()             FORMAT ()           LOG ( float_expression [, base ] )
Q&A




 Best Reference: Books Online
http://msdn.microsoft.com/en-us/library/bb510741.aspx
simplymanas@gmail.com
         @simplymanas
Inspired by
Vinod Kumar, Jacob Sebastian, Pinal Dave

Mais conteúdo relacionado

Semelhante a Top5 functions sql2012

TSQL Functions (SQL Server)
TSQL Functions (SQL Server)TSQL Functions (SQL Server)
TSQL Functions (SQL Server)Steve Stedman
 
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1MariaDB plc
 
New Features of SQL Server 2016
New Features of SQL Server 2016New Features of SQL Server 2016
New Features of SQL Server 2016Mir Mahmood
 
Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Ziaur Rahman
 
SQL Server part 1 (6).pptx
SQL Server part 1 (6).pptxSQL Server part 1 (6).pptx
SQL Server part 1 (6).pptxdeepneuron
 
My Business Intelligence Portfolio
My Business Intelligence PortfolioMy Business Intelligence Portfolio
My Business Intelligence Portfoliolfinkel
 
Jaime\'s Business Intelligence Portfolio
Jaime\'s Business Intelligence PortfolioJaime\'s Business Intelligence Portfolio
Jaime\'s Business Intelligence Portfoliojaimecarlson
 
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_103 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1mlraviol
 
What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?ukdpe
 
SQL Server 2008 Overview
SQL Server 2008 OverviewSQL Server 2008 Overview
SQL Server 2008 OverviewEric Nelson
 
オープンデータを使ったモバイルアプリ開発(応用編)
オープンデータを使ったモバイルアプリ開発(応用編)オープンデータを使ったモバイルアプリ開発(応用編)
オープンデータを使ったモバイルアプリ開発(応用編)Takayuki Goto
 
MDI Training DB2 Course
MDI Training DB2 CourseMDI Training DB2 Course
MDI Training DB2 CourseMarcus Davage
 
IBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql FeaturesIBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql FeaturesKeshav Murthy
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)Jay Patel
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)Jay Patel
 
Uncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony DavisUncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony DavisRed Gate Software
 
Sql server ___________session 2(sql 2008)
Sql server  ___________session 2(sql 2008)Sql server  ___________session 2(sql 2008)
Sql server ___________session 2(sql 2008)Ehtisham Ali
 
The Ring programming language version 1.7 book - Part 31 of 196
The Ring programming language version 1.7 book - Part 31 of 196The Ring programming language version 1.7 book - Part 31 of 196
The Ring programming language version 1.7 book - Part 31 of 196Mahmoud Samir Fayed
 
Cassandra v3.0 at Rakuten meet-up on 12/2/2015
Cassandra v3.0 at Rakuten meet-up on 12/2/2015Cassandra v3.0 at Rakuten meet-up on 12/2/2015
Cassandra v3.0 at Rakuten meet-up on 12/2/2015datastaxjp
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?Andrej Pashchenko
 

Semelhante a Top5 functions sql2012 (20)

TSQL Functions (SQL Server)
TSQL Functions (SQL Server)TSQL Functions (SQL Server)
TSQL Functions (SQL Server)
 
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
 
New Features of SQL Server 2016
New Features of SQL Server 2016New Features of SQL Server 2016
New Features of SQL Server 2016
 
Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012
 
SQL Server part 1 (6).pptx
SQL Server part 1 (6).pptxSQL Server part 1 (6).pptx
SQL Server part 1 (6).pptx
 
My Business Intelligence Portfolio
My Business Intelligence PortfolioMy Business Intelligence Portfolio
My Business Intelligence Portfolio
 
Jaime\'s Business Intelligence Portfolio
Jaime\'s Business Intelligence PortfolioJaime\'s Business Intelligence Portfolio
Jaime\'s Business Intelligence Portfolio
 
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_103 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1
 
What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?
 
SQL Server 2008 Overview
SQL Server 2008 OverviewSQL Server 2008 Overview
SQL Server 2008 Overview
 
オープンデータを使ったモバイルアプリ開発(応用編)
オープンデータを使ったモバイルアプリ開発(応用編)オープンデータを使ったモバイルアプリ開発(応用編)
オープンデータを使ったモバイルアプリ開発(応用編)
 
MDI Training DB2 Course
MDI Training DB2 CourseMDI Training DB2 Course
MDI Training DB2 Course
 
IBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql FeaturesIBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql Features
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
Uncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony DavisUncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony Davis
 
Sql server ___________session 2(sql 2008)
Sql server  ___________session 2(sql 2008)Sql server  ___________session 2(sql 2008)
Sql server ___________session 2(sql 2008)
 
The Ring programming language version 1.7 book - Part 31 of 196
The Ring programming language version 1.7 book - Part 31 of 196The Ring programming language version 1.7 book - Part 31 of 196
The Ring programming language version 1.7 book - Part 31 of 196
 
Cassandra v3.0 at Rakuten meet-up on 12/2/2015
Cassandra v3.0 at Rakuten meet-up on 12/2/2015Cassandra v3.0 at Rakuten meet-up on 12/2/2015
Cassandra v3.0 at Rakuten meet-up on 12/2/2015
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?
 

Último

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

Top5 functions sql2012

  • 1. Top 5 Built-in Functions (Transact-SQL) Manas Ranjan Dash @simplymanas
  • 2. Agenda Programmability Enhancements (Database Engine) select (my) top 5 * from SQL_BuiltIn_Functions **All the function described in this session are purely related to SQL2012. There is no resemblance with SQL2005 or SQL2008. Compatibility level 110
  • 3. What is today’s date? What is tomorrow’s date? SELECT GETDATE() SELECT GETDATE()+1
  • 4. Beginning of the month? SELECT CAST ( CAST (YEAR(GETDATE()) AS VARCHAR) + '-'+ CAST(MONTH(GETDATE()) AS VARCHAR) + '-'+ '01' AS DATE)
  • 5. Beginning of the month? I am from SQL 2012 SELECT CAST( CONCAT( YEAR(GETDATE()),'-', MONTH(GETDATE()),'-', '01') AS DATE)
  • 6. Display Full Name SQL SERVER 2012 SELECT SELECT CONCAT( FIRSTNAME +' ' + ISNULL(FIRSTNAME,'') +' ' + FIRSTNAME,' ', MIDDLENAME + ' ' + ISNULL(MIDDLENAME,'') + ' ' + MIDDLENAME,' ', LASTNAME AS FULLNAME AS FULLNAME ISNULL(LASTTNAME,'') LASTNAME FROM PERSON.PERSON ) AS FULLNAME FROM PERSON.PERSON
  • 7. CONCAT() Function BOL  CONCAT ( string_value1, string_value2 [, string_valueN ] )  All arguments are implicitly converted to string types and then concatenated. BACHCHAN  Null values are implicitly converted to an empty string.  If all the arguments are null, an empty string of type varchar(1) is returned. TOP 1
  • 8. Beginning of the month? SQL SELECT CAST ( SERVER 2012 CONCAT SELECT ( DATEFROMPARTS ( YEAR(GETDATE()),'-', YEAR(GETDATE()), MONTH(GETDATE()),'-', MONTH(GETDATE()), '01' 1) ) AS DATE)
  • 9. DATEFROMPARTS() Function BOL  DATEFROMPARTS ( year, month, day )  DATEFROMPARTS returns a date value with the date portion BACHCHAN set to the specified year, month and day. TOP 2
  • 10. End of the month? SELECT DATEADD SQL SELECT DATEADD (D,-1, SERVER (D,-1, DATEADD 2012 DATEFROMPARTS( SELECT CASE WHEN MONTH(GETDATE())<12 DATEFROMPARTS( (D,-1, YEAR(GETDATE()) THEN DECEMBER IIF(MONTH(GETDATE())<12, DATEFROMPARTS(YEAR(GETDATE()), ELSE YEAR(GETDATE())+1 YEAR(GETDATE()),YEAR(GETDATE()+1)), END, MONTH(GETDATE())+1,1) MONTH(GETDATE())+1), IIF(MONTH(GETDATE())=12,1, CASE WHEN MONTH(GETDATE())=12 THEN 1 ) 1) ELSE MONTH(GETDATE())+1 ) END, 1) )
  • 11. IIF() Function BOL  IIF ( boolean_expression, true_value, false_value)  IIF is translated into CASE BACHCHAN  IIF statements can also be nested only up to the maximum level of 10 like CASE. TOP 3
  • 12. End of the month? SQL --THIS MONTH SERVER END SELECT DATEADD SELECT EOMONTH(GETDATE()) 2012 (D,-1, --NEXT MONTH END DATEFROMPARTS( SELECT EOMONTH(GETDATE(),1) IIF(MONTH(GETDATE())<12, SELECT EOMONTH(GETDATE(),2) YEAR(GETDATE()),YEAR(GETDATE()+1)), SELECT EOMONTH(GETDATE(),14) IIF(MONTH(GETDATE())=12,1, MONTH(GETDATE())+1), --PREVIOUS MONTH END 1) SELECT EOMONTH(GETDATE(),-1) ) SELECT EOMONTH(GETDATE(),-2) SELECT EOMONTH(GETDATE(),-14)
  • 13. EOMONTH() Function BOL  EOMONTH ( start_date [, month_to_add ] )  Even if you pass a string parameter you will get a date return type as it does a implicit conversion. BACHCHAN DECLARE @date VARCHAR(255) SET @date = '12/1/2011' SELECT EOMONTH ( @date ) AS Result GO TOP 4
  • 14. Lets Format a Date SELECT CONVERT(VARCHAR, GETDATE(), 101) --11/23/2012 SELECT CONVERT(VARCHAR, GETDATE(), 102) --2012.11.23 SELECT CONVERT(VARCHAR, GETDATE(), 103) --23/11/2012 SQL SELECT CONVERT(VARCHAR, GETDATE(), 104) --23.11.2012 SERVER SELECT CONVERT(VARCHAR, 2012 GETDATE(), 105) --23-11-2012 SELECT CONVERT(VARCHAR, GETDATE(), 108) --10:04:52 SELECT FORMAT( GETDATE(), 'yyyy-MM-dd HH:MM') --2012-11-23 10:11 SELECT FORMAT( GETDATE(),' "First SQLUG Meet On" ddd dd"th" MMMM yyyy "at" HH:MM ') -- First SQLUG Meet On Fri 24th November 2012 at 10:11
  • 15. FORMAT() Function BOL  FORMAT ( value, format [, culture ] ) BACHCHAN  Use the FORMAT function for locale-aware formatting of date/time and number values as strings. For general data type conversions, use CAST or CONVERT. TOP 5
  • 16. CULTURE  You can use the SET LANGUAGE statement.  Culture accepts any culture supported by the .NET Framework as an argument;  it is not limited to the languages explicitly supported by SQL Server DECLARE @d DATETIME = '01/01/2011'; SELECT FORMAT ( @d, 'd', 'en-US' ) AS 'US English Result' ,FORMAT ( @d, 'd', 'en-gb' ) AS 'Great Britain English Result' ,FORMAT ( @d, 'd', 'de-de' ) AS 'German Result' ,FORMAT ( @d, 'd', 'zh-cn' ) AS 'Simplified Chinese (PRC) Result';
  • 17. NEWLY ADDED FUNCTIONS Conversion functions PARSE () TRY_CONVERT () TRY_PARSE () Date and time functions DATEFROMPARTS () DATETIME2FROMPARTS () Logical functions DATETIMEFROMPARTS () CHOOSE () DATETIMEOFFSETFROMPARTS () IIF () The existing LOG (Transact- EOMONTH () String functions SQL) function now has an SMALLDATETIMEFROMPARTS () CONCAT () optional second base parameter. TIMEFROMPARTS () FORMAT () LOG ( float_expression [, base ] )
  • 18. Q&A Best Reference: Books Online http://msdn.microsoft.com/en-us/library/bb510741.aspx
  • 19. simplymanas@gmail.com @simplymanas
  • 20. Inspired by Vinod Kumar, Jacob Sebastian, Pinal Dave