How to convert varchar to int in MSSQL?

Hi,
I am using mssql. how can i convert varchar to int type.
for eg.
id (int) txt(varchar)
1 ---------10
2 ---------10a
3 ---------10b
i want to convert varchar to int. HOw? pls explain with query.
Thanks
edward

"10a" wouldn't normally be considered a number. So what sort of number do you expect it to be?
Other than that you can look at the substring and convert/cast functions.

Similar Messages

  • How to convert String to int in JSP?

    Hi,
    I set a session attribute in Servlet and want use it in JSP, How can I convert it to int or Integer?
    the line in my code doesn't work:
    int quantity=(int)session.getAttribute("vehiclequantity") ;
    Thanks in advance.
    Wolf
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    <HTML>
    <HEAD>
    <TITLE>Using the for Statement</TITLE>
    </HEAD>
    <BODY>
    <H1>Using the for Statement</H1>
    <%=session.getAttribute("vehiclequantity") %>;
    <%
    int loopIndex;
    int quantity=(int)session.getAttribute("vehiclequantity") ;
    out.println(quantity);
    for (loopIndex = 1; loopIndex <=2; loopIndex++) {
    out.println("This is iteration number "
    + loopIndex + "<BR>");
    %>
    </BODY>
    </HTML>

    Learning how to read errors and understand them will help you solve your problems quicker by yourself... So lets take a look at the error and classes involved...
    The error says:
    "Cannor Resolve Symbol: method valueOf(java.lang.Object) in the class java.lang.Integer" and gives you line line where the error occurs: Integer quantity = Integer.valueOf(session.getAttribute("vehiclequantity"));
    Now, if we look at the API for the Integer we notice that are are only two valueOf methods: valueOf(java.lang.String s) and valueOf(java.lang.String s, int radix). Not valueOf(java.lang.Object) method.
    Now we look at the getAttribute(java.lang.String name) method of HttpSession we see that the method returns a java.lang.Object. Now, you know you put a java.lang.String into that attribute, but the get method returns an Object. This is because you could have put any object in there, an Integer, a String, or some other class instance. But you know it is a String, so you can cast the returned value to a String, so that you will be calling the valueOf(java.lang.String s) method of Integer with the Object returned from the HtttpSession's getAttribute(java.lang.String name) method:
    Integer quantity = Integer.valueOf((String)session.getAttribute("vehiclequantity"));

  • How to convert varchar to date datatype while insert or update in table

    Hai All
    I need to convert to varchar to date.
    I have two Tables T1,T2
    T1 Structure
    Code varchar
    Time varchar
    Date varchar
    T2 Structure
    Empname var
    Empcode var
    Intime date
    Outtime date
    Intrin date
    Introut date
    Att_date
    Now i need to move Time form T1 to T2 Intime,outtime,intrin,introut according some condition
    So now i need to convert Varchar to Date while insert or update
    I have tried something
    Insert into T1 (code,intime,att_date)values
    (code,To_date(Date||time,'dd-mon-yyyy hh24mi'),att_date);
    OR While update
    Update T2 set Outtime=To_date(Date||time,'dd-mon-yyyy hh24mi') where...
    I got an error Ora-01861
    Regards
    Srikkanth.M

    You didn't show any example of your date or time values, butyou might need to add a space between them, like
    To_date(Date || ' ' || time,'dd-mon-yyyy hh24mi')

  • How to convert varchar to timestamp

    in the db , i have a column indtalledDate with datatype varchar-- and value is like 'Thu Dec 20 07:33:20 PST 2012'. Now i need to convert this to timestamp format-->20-DEC-12 07.47.49.463000000 AM . how can i do?
    can anyone pls help???

    >
    hi..Thanks for the quick reply. yes it worked. Now i do need to determine the day in month, quarter span, month span,year start date and year end date..
    something like below:
    to_date(add_months(trunc(installeddate,'q'),3)-1) - to_date(trunc(installeddate,'q')) as quarter_time_span
    any idea how to do this?
    You already have an idea of how to do that
    select trunc(sysdate, 'yyyy') year_start,
       add_months(trunc(sysdate, 'yyyy'), 12) next_year_start,
       trunc(sysdate, 'q') quarter_start,
       add_months(trunc(sysdate, 'q'), 3) next_quarter_start,
       to_number(to_char(sysdate, 'dd')) day_in_month,
       trunc(sysdate, 'mm') month_start,
       add_months(trunc(sysdate, 'mm'), 1) next_month_start from dual
    YEAR_START,NEXT_YEAR_START,QUARTER_START,NEXT_QUARTER_START,DAY_IN_MONTH,MONTH_START,NEXT_MONTH_START
    1/1/2012,1/1/2013,10/1/2012,1/1/2013,20,12/1/2012,1/1/2013

  • How to Convert array of int into array of byte - please help

    I have a 2-dim array of type int and I want to convert it to an array
    of byte. How can I do that? Also, if my 2-dim int array is one dimention
    can I use typecast as in the example below?
    private byte[] getData()
    byte []buff;
    int []data = new int[5];
    //populate data[]
    buff = (byte[]) data; <---- CAN I DO THIS??????
    return buff;

    hi, I suggest you make an array of byte arrays
    --> Byte[][] and use one row for each number
    you can do 2 things,
    take each cipher of one number and put one by one in each column of the row correspondent to that number. of course it may take too much room if the int[] is too big, but that is the easiest way I think
    the other way is dividing your number into bitsets(class BitSet) with sizes of 8 bits and then you can save each bit into each column of your array. and you still have one number in each row. To put your numbers back use the same class.
    Maybe someone has an easier way, I couldnt think of any.

  • How to convert varchar to BLOB in oracle 10g?

    Hi all,
    I have 2 columns A and B which are of varchar2(2000) dataype.
    I would like to concatinate these 2 columns and convert them into BLOB in oracle 10g.
    Any help is appreciated.
    Regards,
    Ravi

    don't use BLOB to store large text, use CLOB instead
    anyway:
    SQL> create table test
      2  (txt varchar2(10)
      3  ,other varchar2(10)
      4  );
    Table created.
    SQL>
    SQL> insert into test values ('some text', 'other text');
    1 row created.
    SQL>
    SQL> create table test2
      2  (col blob)
      3  /
    Table created.
    SQL>
    SQL> insert into test2
      2  select utl_raw.cast_to_raw (txt||other)
      3    from test
      4  /
    1 row created.
    SQL> select *
      2    from test2
      3  /
    SP2-0678: Column or attribute type can not be displayed by SQL*Plus
    SQL>
    SQL> select utl_raw.cast_to_varchar2(col)
      2    from test2
      3  /
    UTL_RAW.CAST_TO_VARCHAR2(COL)
    some textother text
    SQL>
    SQL> drop table test
      2  /
    Table dropped.
    SQL> drop table test2
      2  /
    Table dropped.

  • How to convert VARCHAR to SMALLINT using DB2?

    Hi, guys:
    I am trying to do some comparison in my sql code:
    A.mySmallInt = B.mySmallIntAsString
    Unfortunately, mySmallInt is stored as 1, 2, 3, ..... while
    mySmallIntAsString is stored as '1', '2', '3'......
    I am wondering if there is a DB2 function that can directly
    convert a string into an interger or vice versa?
    thanks a lot

    I believe you can use CAST(B.mySmallIntAsString AS DECIMAL).
    Hi, guys:
    I am trying to do some comparison in my sql code:
    A.mySmallInt = B.mySmallIntAsString
    Unfortunately, mySmallInt is stored as 1, 2, 3, .....
    while
    mySmallIntAsString is stored as '1', '2', '3'......
    I am wondering if there is a DB2 function that can
    directly
    convert a string into an interger or vice versa?
    thanks a lot

  • How to convert varchar to date?

    Hello everyone, I'm new to OBIEE and I'm running into a roadblock...
    I have a date that is stored in a varchar2 column in the physical table, like so: 20071225
    Silly I know, but that's what I'm dealing with.
    Anyway, I want to create a logical column in the business model (using the Administration tool) and convert it to a date so that I can do date arithmetic with it, but TO_DATE doesn't seem to be supported.
    In Oracle SQL would do this (I'm using XE):
    SELECT SYSDATE - TO_DATE('20050101', 'YYYYMMDD') FROM DUAL
    Is there any way to accomplish this same thing in the OBIEE? I see the CAST function, but there doesn't seem to be any way to cast a string to a date?

    The original post is 4 years old - I am sure the poster must have figured it out by this time... obiee 11g supports a To_Datetime function though! thanks!

  • How to convert MSSQL stored procedure to PostgreSQL

    Hi,
    Anyone can help me...?
    How to convert MSSQL stored procedure to PostgreSQL function?
    Is there any tool available to convert T-SQL from MSSQL to PostgreSQL?
    Thanks in advance

    Hello
    Here, I write one sample stored procedure of SQL Server which I need to convert into PostgreSQL. Please help me in this with PostgreSQL.
    Thanks in advance.
    Below is sample SQL Server stored procedure: require to convert into PostgreSQL stored procedure
    CREATE PROCEDURE [dbo].[usp_GetData_ByTableName]
    @TableName NVARCHAR(MAX)
    ,@IncludeKeepAlive BIT
    ,@RowsAffected BIGINT=0 OUTPUT
    ) AS
    BEGIN
    SET NOCOUNT ON
    DECLARE @SQL VARCHAR(MAX)
    Select data base on parameter.
    SET @SQL =
    SELECT *FROM '+@TableName+'
    WHERE 1=1
    +
    CASE WHEN (@IncludeKeepAlive = 0)
    THEN
    AND [MessageTransactionID] <> 152
    ELSE
    END
    EXECUTE SP_EXECUTESQL @SQL
    RETURN 0

  • How to convert date in varchar format to TimeZone format?

    Hi,
    Iam having a date '14-05-08 09:46:35 AM' (in Varchar format). How to convert this date to TimeZone ('IYYY-MM-DD"T"HH12:MI:SS"Z"FX') format?
    For Example,
    My input date is '14-05-08 09:46:35 AM' and I need output as '2008-05-14T09:46:35Z'.
    How to convert this?
    Thanks in Advance.

    Hi,
    You would look up the TO_TIMESTAMP and the TO_CHAR functions in the SQL reference manual for the version you don't mention, and make up the solution yourself, rather than asking someone else to do your homework.
    Sybrand Bakker
    Senior Oracle DBA

  • How to convert an int variable into Number

    Hi all,
    I am using Jdeveloper 11.1.1.2
    When I insert a row in a entity object I want to create another row in another entity object, but when I am setting the attribute for the new row I have a problem.
    Here is the code:
    int a = EO1Impl.paramA;
    NameEO2Impl.setAttribute("Attribute1", a);
    The error is that variable a is an int, while Attribute1 is a Number.
    How can convert an int to a Number.
    Thank you.

    Andrea9,
    If a, as you say is an int, and assuming you mean an oracle.jbo.domain.Number,
    NameEO2Impl.setAttribute("Attribute1", new oracle.jbo.domain.Number(a));works just fine.
    John

  • How to convert an int variable into String type

    hi everybody
    i want to know how to convert an interger variable into string variable
    i have to implement a code which goes like this
    Chioce ch;
    for(int i=0;i<32;i++)
    // here i need a code to convert the int variable i into a string variable
    ch.add(String variable);
    how do i convert that int variable i into a String type variable??
    can anyone help me?

    Different methods:
    int a;
    string s=a+"";or
    String.valueOf(int) is the better option because Int.toString() generated an intermediate object to get the endresult
    Ema

  • Can u tell me how to convert int value in to string

    hi to all, can u tell me how to convert int value in to string.

    hi to all, can u tell me how to convert int value in
    to string.Even this way:
    int number = 155;
    String mystring = ""+155;
    [\code]                                                                                                                                                                                                                                                                                               

  • How to convert an int number to a byte array, and how to convert back?

    Hello, everybody,
    Just as the topic, if I have an int number(which should have 32-bit, or 4-byte), how can I convert it into a byte array. And also, how to convert it back.
    Thank you in advance!

    Alternatively you can use ByteBuffers to do this work for you
    // to a Byte Array
    int originalInt =...;
    ByteBuffer myBuffer = ByteBuffer.allocate(4);
    myBuffer.putInt( originalInt );
    byte myArray[] = myBuffer.array();
    // and back to an int
    ByteBuffer myOtherBuffer = ByteBuffer.wrap(myArray);
    int finalInt = myOtherBuffer.getInt();

  • How to convert from int to string

    Can anyone help me on how to convert int to string?
    Thanks,
    Q

    int i = 3
    String S = i + ""
    i will be promoted to String automatically when the above expression is evaluated

Maybe you are looking for