How to covert INT to VARCHAR in SQL condition

18,871

Solution 1

Try this:

select... from... where cast(R.WORK_YM as varchar(10))= '201611'

Solution 2

Try this Query for SQL Server,

Use CONVERT function

select * from Table R where CONVERT(varchar(10), R.WORK_YM) = '201611'

OR

Use STR function

select * from Table R where STR(R.WORK_YM,10) = '201611'

OR

Use CAST function

select * from Table R where CAST(R.WORK_YM as varchar(10)) = '201611'

For MySQL,

Use CAST function

select * from Table R where CAST(R.WORK_YM as Char(10)) = '201611'

Use CONVERT function

select * from Table R where CONVERT(Char(10), R.WORK_YM) = '201611'
Share:
18,871
Dang Anh
Author by

Dang Anh

Updated on June 07, 2022

Comments

  • Dang Anh
    Dang Anh almost 2 years

    select... from... where R.WORK_YM = 201611

    how to change R.WORK_YM to String value?

  • Dang Anh
    Dang Anh over 7 years
    It's not work for my sql and here is the error: [Error Code: -461, SQL State: 42846] DB2 SQL error: SQLCODE: -461, SQLSTATE: 42846, SQLERRMC: SYSIBM.INTEGER;SYSIBM.VARCHAR ..
  • Dang Anh
    Dang Anh over 7 years
    I tried every query you posted but it's still not work and the error is: Error Code: -461, SQL State: 42846] DB2 SQL error: SQLCODE: -461, SQLSTATE: 42846, SQLERRMC: SYSIBM.INTEGER;SYSIBM.VARCHAR
  • Ranjana Ghimire
    Ranjana Ghimire over 7 years
    Its for mssql . Try this for mysql : select... from... where cast(R.WORK_YM as char(10))= '201611'
  • Dang Anh
    Dang Anh over 7 years
    my job is doing on mssql Beauty!
  • Ranjana Ghimire
    Ranjana Ghimire over 7 years
    Did the next query work?
  • Dang Anh
    Dang Anh over 7 years
    YES! IT WORKS, THANK YOU xD
  • Dang Anh
    Dang Anh over 7 years
    no worries , I got the answer from Ranjana Ghimire: where cast(R.WORK_YM as char(10))= '201611' Thank you!
  • Ranjana Ghimire
    Ranjana Ghimire over 7 years
    Can you please submit my comment as answer :)
  • Kushan
    Kushan over 7 years
    ohh you are using mysql. ok great