Solved

10774: PARSE or TRY_PARSE

Posted on 2014-11-28
5
93 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
  • 3
  • 2
5 Comments
 
LVL 8

Assisted Solution

by:vr6r
vr6r earned 500 total points
Comment Utility
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
Comment Utility
can you give T-SQL please
0
 
LVL 8

Accepted Solution

by:
vr6r earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now