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. How to replace string with stuff ?

How to replace string with stuff ?

Scheduled Pinned Locked Moved Database
tutorialquestion
5 Posts 3 Posters 0 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.
  • C Offline
    C Offline
    caulsonchua
    wrote on last edited by
    #1

    Dear All, May i know how to replace other table time in to other date time Select EP_SCAN_DATE,CAST(STUFF(STUFF(EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME) as SCANDATE FROM AcmkIMS.dbo.EP_SCAN_HIST Result : 2012-01-06 04:39:19.000 Want actual result: example time is 09:00 replace in 2012-01-06 09:00 kindly advise

    Richard DeemingR M 2 Replies Last reply
    0
    • C caulsonchua

      Dear All, May i know how to replace other table time in to other date time Select EP_SCAN_DATE,CAST(STUFF(STUFF(EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME) as SCANDATE FROM AcmkIMS.dbo.EP_SCAN_HIST Result : 2012-01-06 04:39:19.000 Want actual result: example time is 09:00 replace in 2012-01-06 09:00 kindly advise

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

      Don't store dates as strings, and don't use string methods to manipulate dates. If you have any control over this database, you should change the EP_SCAN_DATE column to be a datetime. You haven't specified which DBMS you're using. Assuming MS SQL, you can use something like this:

      SELECT DateAdd(hour, 9, DateDiff(dd, 0, YourDateTimeColumn))

      The DateDiff(dd, 0, X) will return just the date part of X, with the time set to midnight. You then use DateAdd to add as many hours, minutes and seconds as you require.


      "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

      C 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Don't store dates as strings, and don't use string methods to manipulate dates. If you have any control over this database, you should change the EP_SCAN_DATE column to be a datetime. You haven't specified which DBMS you're using. Assuming MS SQL, you can use something like this:

        SELECT DateAdd(hour, 9, DateDiff(dd, 0, YourDateTimeColumn))

        The DateDiff(dd, 0, X) will return just the date part of X, with the time set to midnight. You then use DateAdd to add as many hours, minutes and seconds as you require.


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

        C Offline
        C Offline
        caulsonchua
        wrote on last edited by
        #3

        HI, i used MS SQL server 2008

        Richard DeemingR 1 Reply Last reply
        0
        • C caulsonchua

          HI, i used MS SQL server 2008

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

          In that case, you might want to consider changing the column type to datetime2(0)[^], which is two bytes smaller than a datetime. Alternatively, if you never need the time portion of the column, you could change the type to date[^], which is even smaller. You'd then need to cast the value to datetime2(0) before adding the time:

          SELECT DateAdd(hour, 9, Convert(datetime2(0), YourDateColumn))


          "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
          • C caulsonchua

            Dear All, May i know how to replace other table time in to other date time Select EP_SCAN_DATE,CAST(STUFF(STUFF(EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME) as SCANDATE FROM AcmkIMS.dbo.EP_SCAN_HIST Result : 2012-01-06 04:39:19.000 Want actual result: example time is 09:00 replace in 2012-01-06 09:00 kindly advise

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

            Let me reinforce what Richard has said, NEVER store date as text/varchar/char, ALWAYS use the correct data type. Text will give you endless problems whenever you try and manipulate the data. The date and datetime data types will give you the tools to manipulate the data easily (not simply it can take a bit of thinking to work out how to achieve a given result).

            Never underestimate the power of human stupidity RAH

            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