Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Throw error if date fails the conversion or invalid date string

Throw error if date fails the conversion or invalid date string

Scheduled Pinned Locked Moved Database
helpdatabase
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    indian143
    wrote on last edited by
    #1

    Hi All, I am using a stored proc which is using a depending upon that it is pulling some values and make some changes in the database, what I need to do is to throw an error when date conversion from that string or text fails and should not perform further action. Can anybody please help me with this, any code snippet, a link or even a suggestion helps me, thanks in advance. Below is my code

    ALTER procedure [dbo].[TrackUpdatedColumnNamesProc]
    (@DateToTrack nvarchar(max))
    as
    begin
    --declare @DateToTrack nvarchar(max)='2016-03-18'
    delete from TrackUpdatedColumnNames;
    --check here (as soon as conversion of @DateToTrack fails I shouldn't be doing any logic
    --doing the logic here
    end
    GO
    declare @DateToTrack nvarchar(max)='2016-03-18'
    exec [dbo].[TrackUpdatedColumnNamesProc] @DateToTrack
    GO

    Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

    M Richard DeemingR 2 Replies Last reply
    0
    • I indian143

      Hi All, I am using a stored proc which is using a depending upon that it is pulling some values and make some changes in the database, what I need to do is to throw an error when date conversion from that string or text fails and should not perform further action. Can anybody please help me with this, any code snippet, a link or even a suggestion helps me, thanks in advance. Below is my code

      ALTER procedure [dbo].[TrackUpdatedColumnNamesProc]
      (@DateToTrack nvarchar(max))
      as
      begin
      --declare @DateToTrack nvarchar(max)='2016-03-18'
      delete from TrackUpdatedColumnNames;
      --check here (as soon as conversion of @DateToTrack fails I shouldn't be doing any logic
      --doing the logic here
      end
      GO
      declare @DateToTrack nvarchar(max)='2016-03-18'
      exec [dbo].[TrackUpdatedColumnNamesProc] @DateToTrack
      GO

      Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      So use try/catch in the stored proc. However you are not processing sequentially, that requires a cursor or while or a CTE, so you should test the date conversion before performing the delete. Pretty sure there is and IsDate function in TSQL.

      Never underestimate the power of human stupidity RAH

      I 1 Reply Last reply
      0
      • M Mycroft Holmes

        So use try/catch in the stored proc. However you are not processing sequentially, that requires a cursor or while or a CTE, so you should test the date conversion before performing the delete. Pretty sure there is and IsDate function in TSQL.

        Never underestimate the power of human stupidity RAH

        I Offline
        I Offline
        indian143
        wrote on last edited by
        #3

        Yeah sure thank you very much buddy.

        Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

        1 Reply Last reply
        0
        • I indian143

          Hi All, I am using a stored proc which is using a depending upon that it is pulling some values and make some changes in the database, what I need to do is to throw an error when date conversion from that string or text fails and should not perform further action. Can anybody please help me with this, any code snippet, a link or even a suggestion helps me, thanks in advance. Below is my code

          ALTER procedure [dbo].[TrackUpdatedColumnNamesProc]
          (@DateToTrack nvarchar(max))
          as
          begin
          --declare @DateToTrack nvarchar(max)='2016-03-18'
          delete from TrackUpdatedColumnNames;
          --check here (as soon as conversion of @DateToTrack fails I shouldn't be doing any logic
          --doing the logic here
          end
          GO
          declare @DateToTrack nvarchar(max)='2016-03-18'
          exec [dbo].[TrackUpdatedColumnNamesProc] @DateToTrack
          GO

          Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          The simplest option is to change your parameter's type to date. Then, if the caller tries to pass in a value that isn't a date, an error will be thrown, and the procedure will not be executed. You should always use an appropriate data type for the data you're dealing with. Storing dates in strings is a sign of a bad design.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups