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. Web Development
  3. ASP.NET
  4. dateformat

dateformat

Scheduled Pinned Locked Moved ASP.NET
tutorial
7 Posts 4 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.
  • S Offline
    S Offline
    suriyakaqnnan
    wrote on last edited by
    #1

    how to convert the nvarchar datatype to datetime

    S A 2 Replies Last reply
    0
    • S suriyakaqnnan

      how to convert the nvarchar datatype to datetime

      S Offline
      S Offline
      Sandeep Mewara
      wrote on last edited by
      #2

      You can use CONVERT: Here you go: CAST and CONVERT[^] How to format a Date or DateTime in SQL Server[^] Example:

      SELECT CONVERT(VARCHAR, columnName, 103) AS MyDate From MyTable;

      Sandeep Mewara Microsoft ASP.NET MVP [My latest Article]: Server side Delimiters in ASP.NET[^]

      S 1 Reply Last reply
      0
      • S Sandeep Mewara

        You can use CONVERT: Here you go: CAST and CONVERT[^] How to format a Date or DateTime in SQL Server[^] Example:

        SELECT CONVERT(VARCHAR, columnName, 103) AS MyDate From MyTable;

        Sandeep Mewara Microsoft ASP.NET MVP [My latest Article]: Server side Delimiters in ASP.NET[^]

        S Offline
        S Offline
        suriyakaqnnan
        wrote on last edited by
        #3

        thank you friend but i want to convert the varchar dataype to datetime format (ie.)if i enter 22.3.2004 in date of birth text box it should to stored on the database as 22/3/2004 how it possible please reply me and also if the user enter like 22.3.2004 error msg has to display pls reply both

        S A K 3 Replies Last reply
        0
        • S suriyakaqnnan

          thank you friend but i want to convert the varchar dataype to datetime format (ie.)if i enter 22.3.2004 in date of birth text box it should to stored on the database as 22/3/2004 how it possible please reply me and also if the user enter like 22.3.2004 error msg has to display pls reply both

          S Offline
          S Offline
          Sandeep Mewara
          wrote on last edited by
          #4

          suriyakaqnnan wrote:

          i want to convert the varchar dataype to datetime format

          I replied/shared the exact same question. :doh:

          suriyakaqnnan wrote:

          f the user enter like 22.3.2004 error msg has to display

          Why? If needed, put your logic on frontend or UI layer. Validate and then pass on to DB. Try!

          Sandeep Mewara Microsoft ASP.NET MVP [My latest Article]: Server side Delimiters in ASP.NET[^]

          1 Reply Last reply
          0
          • S suriyakaqnnan

            how to convert the nvarchar datatype to datetime

            A Offline
            A Offline
            Albarhami
            wrote on last edited by
            #5

            You can use Convert.to... in C# or CAST('09/25/2004'AS DATETIME)... in SQL Server

            1 Reply Last reply
            0
            • S suriyakaqnnan

              thank you friend but i want to convert the varchar dataype to datetime format (ie.)if i enter 22.3.2004 in date of birth text box it should to stored on the database as 22/3/2004 how it possible please reply me and also if the user enter like 22.3.2004 error msg has to display pls reply both

              A Offline
              A Offline
              Albarhami
              wrote on last edited by
              #6

              For more details about all datatype conversion in C# have a look here: http://www.c-sharpcorner.com/uploadfile/puranindia/type-conversions-in-C-Sharp/[^]

              1 Reply Last reply
              0
              • S suriyakaqnnan

                thank you friend but i want to convert the varchar dataype to datetime format (ie.)if i enter 22.3.2004 in date of birth text box it should to stored on the database as 22/3/2004 how it possible please reply me and also if the user enter like 22.3.2004 error msg has to display pls reply both

                K Offline
                K Offline
                KiranKumar Roy
                wrote on last edited by
                #7

                use below SQL function to convert DD/MM/YYYY string to Datetime.. Make sure every time u pass "DD/MM/YYYY" format only.

                CREATE FUNCTION ChangeDateFormat
                (
                @STRING NVARCHAR(40),
                @DELIMITER NVARCHAR(40)
                )
                RETURNS DateTime
                AS
                BEGIN
                DECLARE @MM as varchar(5)
                DECLARE @DD as varchar(5)
                DECLARE @YY as varchar(5)
                DECLARE @POS INT
                DECLARE @NEXTPOS INT
                DECLARE @INT INT
                DECLARE @TEMP as varchar(50)
                SET @STRING = @STRING + @DELIMITER
                SET @POS = charindex(@Delimiter,@String)
                SET @INT = 1

                WHILE (@POS <> 0)
                BEGIN
                	if @INT = 1 
                		Begin
                			SET @DD = substring(@String,1,@Pos - 1)
                		End
                	if @INT = 2 
                		Begin
                			SET  @MM = substring(@String,1,@Pos - 1)	
                		End
                	if @INT = 3 
                		Begin
                			SET @YY = substring(@String,1,@Pos - 1)	
                		End	
                	
                	SET @STRING = substring(@String,@pos+1,len(@String))
                	SET @POS = charindex(@Delimiter,@String)
                	SET @INT = @int + 1
                END 
                SET @TEMP = @MM+'/'+ @DD + '/' + @YY
                
                Return  convert(datetime, @TEMP)
                

                END

                KiranKumar Roy

                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