Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

10774: PARSE or TRY_PARSE

Posted on 2014-11-28
5
Medium Priority
?
108 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 2000 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 2000 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

719 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