Convert a String to Int Using SQL Query

Convert a string to int using sql query

You could use CAST or CONVERT:

SELECT CAST(MyVarcharCol AS INT) FROM Table

SELECT CONVERT(INT, MyVarcharCol) FROM Table

mySQL - How to convert a string to an int and string to date using sql query

You can convert a string to a number by adding 0 to it.

You can convert a string to a DATETIME using CAST()

SELECT 0 + total_qty AS total_qty, delivery_agent_name, CAST(created_on AS DATETIME) AS created_on, shipment_stage
FROM sompopo_dwh_express.sompopo_express_shipment

Convert a string with operation to int using sql query

If + is the only operator and it appears once, then:

select (case when col like '%+%'
then substring_index(col, '+', 1) + substring_index(replace(col, ' ', ''), '+', -1)
else col + 0
end) as added_value

Convert INT to VARCHAR SQL

Use the convert function.

SELECT CONVERT(varchar(10), field_name) FROM table_name

conversion failed string to int using case when then in sql

seems you'd like to join on the output of the case statement, then you should be this:

SELECT 
CASE
WHEN Grade = 'Diesel' THEN 4
WHEN Grade = 'Petrol' THEN 5
END as Grade
,sales
FROM testTableA as tta
INNER JOIN testTableB ttb
ON ttb.productid = CASE
WHEN tta.Grade = 'Diesel' THEN 4
WHEN tta.Grade = 'Petrol' THEN 5
END

or wrap tta into a subquery to avoid duplicating the same logic

SQL query to replace a string and convert rest of the string to Int

You can't use order by with convert alias

but you can try to use a subquery to get the AccountCodeWithoutDAG then order by it

SELECT  TOP 1 AccountCodeWithoutDAG 
FROM
(
SELECT REPLACE(AccountCode,'DAG','') AS AccountCodeWithoutDAG
FROM Accounts
where MangCode = 'ADFG'
) t1
Order by Convert(int, AccountCodeWithoutDAG )

Convert string to integer and update into other field

Try this query

UPDATE [MyTestData].[dbo].[Addresses]
SET [cityZipID] = (Convert(Int,[zip])/10) -100
WHERE [city] = 'Wien'


Related Topics



Leave a reply



Submit