You must Sign In to post a response.
  • Category: ASP.NET

    How to convert date Varchar Format to Date Format using sql query.

    In my project i need to search records by date.
    that is from DateTime to to i am create the DateTime in Varchar format.
    now i Search records using from and to DateTimeit is not show exact output.
    i thing my created date format is wrong and that is the reason.
    So i want to convert from date and to date from varchar datatype to DateTimeFormat.
    and also which format will be give exact output for me is very important.

    So please help me to how i am Convert already created data type Varchar to DateTime format
    or may i drop the column and recreate a new column with DateTime Format.

    i am having lot of stress with this module friends.

    Thanks with
  • #766360
    you can try this Query

    select CONVERT(varchar(20),date_time,101) from sample2


    select CONVERT(varchar(20),date_time,102) from sample2

    select CONVERT(varchar(20),date_time,103) from sample2

    select CONVERT(varchar(20),date_time,111) from sample2

    select CONVERT(varchar(20),date_time,110) from sample2


    select * from sample2 where CONVERT(varchar(20),date_time,101) between CONVERT(varchar(20),date_time,101) and CONVERT(varchar(20),date_time,101)

    select * from sample2 where CONVERT(varchar(20),date_time,102) between CONVERT(varchar(20),date_time,102) and CONVERT(varchar(20),date_time,102)

    Name : Dotnet Developer-2015
    Email Id :

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766361
    Convert varchar to date:
    1. SELECT CONVERT(datetime, ColumnName, 110)
    FROM TableName
    WHERE CONVERT(datetime, ColumnName, 110) < '2015-08-22 00:00:00.000'
    and CONVERT(datetime, ColumnName, 110) > '2015-08-21 00:00:00.000'

    2. SELECT CAST(ColumnName AS datetime)
    FROM TableName
    WHERE CAST(ColumnName AS datetime) < '2015-08-22 00:00:00.000'
    and CAST(ColumnName AS datetime) > '2015-08-21 00:00:00.000'

    Please find meaning of different different datetime (Eg. 101,102 etc) formats over here:

  • #766363
    you can use SQL CONVERT() function, that converts an expression of one data type to another.
    The following script uses the CONVERT() function to display different formats. We will use the GETDATE() function to get the current date/time

    *SQL Server supports the date format in Arabic style by using the Kuwaiti algorithm.

    you can also use CAST function, which will convert input data in specified date object
    see below link for more details
    hope it helps

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766369

    You can alter the datatype of the column as datetime and then perform the whichever operations you want.
    Use this below query to alter the datatype of the column.
    alter table tab_nmae alter column column_name datetime

    I hope this could help you

  • #766370
    Heartful thanks for all of your valuable and quick reply friends. thanks a lot.


  • #766371

    Rather than convert the column everytime my suggestion is Alter the datatype of the specified column and perform actions as you want.

    ALTER TABLE tablename

    If you still want to convert with the fields while fetching records from database, I would suggest you to use CONVERT function to convert the appropriate format.

    SELECT CONVERT(DATETIME, @varchardate)

    Try something like above to achieve your goal, hope this will helpful to you....

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • Sign In to post your comments