Solved

10774: PARSE or TRY_PARSE

Posted on 2014-11-28
5
96 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
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

947 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

19 Experts available now in Live!

Get 1:1 Help Now