How to supply a string for a datetime column in

Posted on 2009-04-27
Last Modified: 2013-12-17
I want to write a select statement in which I supply a string value for a timestamp column (in Sybase Sql Anywhere  database)

For example:
string dateTimeStr = "2009-04-27 12:34:23";
string selectStr = " Select * from Table where DateTimeColumn =' "+dateTimeStr+" ' ";

When I execute this select statement, the Odbc driver gives an error like :
Can not convert ... to timestamp.
When I use the OdbcParameterCollection of the command and add a c# DateTime type value as the parameter to the select statement, the command executes without error.
Isn't there another way other than using parametres?
It would be really good if I could give a string in the where condition..
Thanks in advance..
Question by:visne
    LVL 15

    Accepted Solution

    I would recommend always using parameters. It may seem cumbersome at first but it far easier for readability, especially coming back to commands you've written 6 months ago.

    The problem is that while some servers can implicitly convert a string to a datetime or timestamp value, other may not (I can't comment on Sybase as I've only used it a handful of times for our phone system), there may be issues with localisation on the server.
    LVL 1

    Assisted Solution

    The following code will work for what you need, in addition it has the benefit protecting you from SQL injected.  You will need to add System.Data and System.Data.SqlClient to your USING's.

                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnl;
                sqlCommand.CommandText = "SELECT * FROM Table WHERE FieldName = @FieldValue";
                sqlCommand.Parameters.Add("@FieldValue", SqlDbType.DateTime).Value = DateTime.Parse("12/1/2009 13:34:20");
                SqlDataReader drRecords = sqlCommand.ExecuteReader();

    Open in new window


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now