Convert INT to VARCHAR SQL
Use the convert function.
SELECT CONVERT(varchar(10), field_name) FROM table_name
How to covert INT to VARCHAR in SQL condition
Try this:
select... from... where cast(R.WORK_YM as varchar(10))= '201611'
SMALLINT to VARCHAR conversion in SELECT / CASE statement in SQL query
So I have recreated your error:
DECLARE @X TABLE (PCM_ID1 smallint)
INSERT INTO @X SELECT 14
INSERT INTO @X SELECT 1
SELECT
CASE
WHEN PCM_ID1 = '0' THEN '0'
WHEN PCM_ID1 = '1' THEN '1'
WHEN PCM_ID1 = '2' THEN '2'
WHEN PCM_ID1 = '3' THEN '3'
WHEN PCM_ID1 = '4' THEN '4'
WHEN PCM_ID1 = '5' THEN '5'
WHEN PCM_ID1 = '6' THEN '6'
WHEN PCM_ID1 = '7' THEN '7'
WHEN PCM_ID1 = '8' THEN '8'
WHEN PCM_ID1 = '9' THEN '9'
WHEN PCM_ID1 = '10' THEN 'A'
WHEN PCM_ID1 = '11' THEN 'B'
WHEN PCM_ID1 = '12' THEN 'C'
WHEN PCM_ID1 = '13' THEN 'D'
WHEN PCM_ID1 = '14' THEN 'E'
WHEN PCM_ID1 = '15' THEN 'F'
WHEN PCM_ID1 = '16' THEN '10'
ELSE PCM_ID1
END AS [PCM ID1 Dec2Hex]
FROM @X
If you that you will get the same error. The reason is because the ELSE
in your case statement returns a smallint field which makes the field [PCM ID1 Dec2Hex]
smallint and you get the error.
If you cast the else as a varchar it will fix this issue. The problem is that it is trying to convert the E back to smallint. I hope this makes sense.
DECLARE @X TABLE (PCM_ID1 smallint)
INSERT INTO @X SELECT 14
INSERT INTO @X SELECT 1
SELECT
CASE
WHEN PCM_ID1 = '0' THEN '0'
WHEN PCM_ID1 = '1' THEN '1'
WHEN PCM_ID1 = '2' THEN '2'
WHEN PCM_ID1 = '3' THEN '3'
WHEN PCM_ID1 = '4' THEN '4'
WHEN PCM_ID1 = '5' THEN '5'
WHEN PCM_ID1 = '6' THEN '6'
WHEN PCM_ID1 = '7' THEN '7'
WHEN PCM_ID1 = '8' THEN '8'
WHEN PCM_ID1 = '9' THEN '9'
WHEN PCM_ID1 = '10' THEN 'A'
WHEN PCM_ID1 = '11' THEN 'B'
WHEN PCM_ID1 = '12' THEN 'C'
WHEN PCM_ID1 = '13' THEN 'D'
WHEN PCM_ID1 = '14' THEN 'E'
WHEN PCM_ID1 = '15' THEN 'F'
WHEN PCM_ID1 = '16' THEN '10'
ELSE CAST(PCM_ID1 AS VARCHAR)
END AS [PCM ID1 Dec2Hex]
FROM @X
How to cast or convert numeric to varchar data type on sybase?
The error messages mention implicit conversion errors so this would tend to rule out the explicit conversions being performed by the cast()
calls.
This leaves us with datatype mismatch issues for the other columns/values; OP will want to doublecheck the datatypes of the recipients
columns:
the 1st error message mentions
numeric to varchar
and since the only numeric values are@k
and@acc
, I'm guessing either theid
orinn
column is defined asvarchar
the 2nd error message mentions
integer to varchar
and since Sybase (ASE) treats1
and300335
as integers, I'm guessing either theclient_id
,bill
orversion
column is defined asvarchar
SQL Converting int to varchar
Try this. All of that excitement in the function may be unnecessary.
CONVERT(varchar(10),(@CurrentDateTime-@Start_Time),108)
How to Convert a Month from INT to VARCHAR when grouping the date by MONTH
For MySql it would be:
SELECT
monthname(i.Date) AS Month,
Year(i.Date) AS Year,
Count(i.Id) AS `Number of Interviews`
FROM Interviews i
GROUP BY month(i.Date), monthname(i.Date), year(i.Date)
ORDER BY year(i.Date) DESC, month(i.Date) ASC
For SQL Server:
SELECT
datename(month, i.date) AS Month,
Year(i.Date) AS Year,
Count(i.Id) AS [Number of Interviews]
FROM Interviews i
GROUP BY month(i.Date), datename(month, i.date), year(i.Date)
ORDER BY year(i.Date) DESC, month(i.Date) ASC
How to convert integer into datetime in varchar format
You need a space, and a colon. I'm not sure why you want to do this though... present the date in a format on your front end (presentation layer) and keep dates stored as dates or datetime in the database and you won't run into this issue :)
SELECT CONVERT(VARCHAR(MAX), CAST('20090104 14:21' AS DATETIME))
Also, no need to use MAX
here. That's a waste of storage. Something like this makes more sense.
SELECT CONVERT(VARCHAR(24), CAST('20090104 14:21' AS DATETIME), 113)
Using a column name...
SELECT CONVERT(VARCHAR(24), CAST(YourColumnName AS DATETIME), 113)
FROM YourTable
You can see other conversions here
Conversion from INT to varchar in sql
As already pointed out in the comments, you can try to use a CASE
expression
SELECT CASE
WHEN nmuloc = 0 THEN
'0'
ELSE
convert(varchar(10),
convert(date,
convert(varchar(8),
nmuloc),
112),
103)
END
FROM elbat;
or try_convert()
and coalesce()
.
SELECT coalesce(convert(varchar(10),
try_convert(date,
convert(varchar(8),
nmuloc),
112),
103),
'0')
FROM elbat;
db<>fiddle
The latter one will also correct other "malformed" data like 123
for example. The former will also fail in such cases. You may want that or not.
But, as also already pointed out in the comments, your real problem is that you use an inappropriate data type. Change the column's datatype to some date/time data type to really fix this.
Related Topics
Delete All Data in SQL Server Database
Storing Datetime (Utc) VS. Storing Datetimeoffset
Count Distinct with Conditions
How to Get the Actual Stored Procedure Line Number from an Error Message
Change Schema Name of Table in SQL
When to Use a View Instead of a Table
Why No Many-To-Many Relationships
What Are the Uses for Cross Join
Differencebetween Cube, Rollup and Groupby Operators
Check for Changes to an SQL Server Table
Add Primary Key to Existing Table
Efficient Way of Getting @@Rowcount from a Query Using Row_Number
When Should I Use Stored Procedures
In SQL Server, When Should You Use Go and When Should You Use Semi-Colon ;
Grant Execute Permission for a User on All Stored Procedures in Database
Can SQLite Handle 90 Million Records