Solved

10774: PARSE or TRY_PARSE

Posted on 2014-11-28
5
101 Views
Last Modified: 2014-11-29
Hi experts:

i am reading about PARSE and TRY_PARSE
http://www.mssqltips.com/sqlservertip/2564/new-data-type-conversion-functions-in-sql-server-2012/

I do not understand in what cases should use either
0
Comment
Question by:enrique_aeo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 8

Assisted Solution

by:vr6r
vr6r earned 500 total points
ID: 40471168
If Parse fails, it will error out and your sql statement will fail.

Try_Parse on the other hand will return Null if it is not able to successfully perform the parse, but will not error out.

In practice you might use it in an example like this.  Say you have a script that attempts to parse a string to a datetime but your source doesn't always provide a string.  Using Try_Parse, you could provide a default date in the event the Try_Parse call returns NULL.
0
 

Author Comment

by:enrique_aeo
ID: 40471367
can you give T-SQL please
0
 
LVL 8

Accepted Solution

by:
vr6r earned 500 total points
ID: 40471453
Sure, for this example say we want to parse a 'last active' date string, but in the event that fails we want to fall back to an 'account creation' datetime that we can always expect to be populated.

This will try to parse the string but will return NULL if that fails, which the COALESCE expression will catch and will instead use the account_creation field, so we can count on our resulting query to always have a "lastactive" column populated.

SELECT
   users.firstname,
   users.lastname,
   COALESCE(TRY_PARSE(users.last_active AS datetime), users.account_creation) as lastactive
FROM users

Open in new window

0
 

Author Comment

by:enrique_aeo
ID: 40471484
I have this table
CREATE TABLE [HumanResources].[Employee_Parse](
      [EmployeeID] [int] IDENTITY(1,1) NOT NULL,
      [NationalIDNumber] [nvarchar](15) NOT NULL,
      [ContactID] [int] NOT NULL,
      [LoginID] [nvarchar](256) NOT NULL,
      [ManagerID] [int] NULL,
      [Title] [nvarchar](50) NOT NULL,
      [BirthDate] [datetime] NULL,
      [MaritalStatus] [nchar](1) NOT NULL,
      [Gender] [nchar](1) NOT NULL,
      [HireDate] [datetime] NULL,
      [SalariedFlag] [dbo].[Flag] NOT NULL,
      [VacationHours] [smallint] NOT NULL,
      [SickLeaveHours] [smallint] NOT NULL,
      [CurrentFlag] [dbo].[Flag] NOT NULL,
      [rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
      [ModifiedDate] [datetime] NULL)

I am try this
Use AdventureWorks
GO                                          
SELECT
   [NationalIDNumber],
   [LoginID],
   COALESCE(TRY_PARSE([BirthDate] AS datetime),[ModifiedDate] ) as [Modified]
FROM [HumanResources].[Employee_Parse]

but i have this error
Msg 8116, Level 16, State 1, Line 1
Argument data type datetime is invalid for argument 1 of parse function.
0
 

Author Comment

by:enrique_aeo
ID: 40471609
it is OK
CREATE TABLE [HumanResources].[Employee_Parse](
      [EmployeeID] [int]  NOT NULL,
      [NationalIDNumber] [nvarchar](15) NOT NULL,
      [ContactID] [int] NOT NULL,
      [LoginID] [nvarchar](256) NOT NULL,
      [ManagerID] [int] NULL,
      [Title] [nvarchar](50) NOT NULL,
      [BirthDate] [nvarchar](50) NULL,
      [MaritalStatus] [nchar](1) NOT NULL,
      [Gender] [nchar](1) NOT NULL,
      [HireDate] [nvarchar](50) NULL,
      [SalariedFlag] [dbo].[Flag] NOT NULL,
      [VacationHours] [smallint] NOT NULL,
      [SickLeaveHours] [smallint] NOT NULL,
      [CurrentFlag] [dbo].[Flag] NOT NULL,
      [rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
      [ModifiedDate] [nvarchar](50) NULL)


INSERT INTO [HumanResources].[Employee_Parse]
               SELECT *
               FROM [HumanResources].[Employee]

update [HumanResources].[Employee_Parse]
set [ModifiedDate] = GETDATE()

update [HumanResources].[Employee_Parse]
set [BirthDate] = ' '
where [EmployeeID] > 200

SELECT
   [NationalIDNumber],
   [LoginID],
   [BirthDate],
   TRY_PARSE([BirthDate] AS datetime) TRY_PARSE_BirthDate,
   COALESCE(TRY_PARSE([BirthDate] AS datetime),[ModifiedDate] ) as [Modified]
FROM [HumanResources].[Employee_Parse]
order by [EmployeeID] desc
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question