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
How to Select Only the First Rows for Each Unique Value of a Column
Doesn't Linq to SQL Miss the Point? Aren't Orm-Mappers (Subsonic, etc.) Sub-Optimal Solutions
SQL Update Fields of One Table from Fields of Another One
Oracle 'Partition By' and 'Row_Number' Keyword
Bigquery SQL for 28-Day Sliding Window Aggregate (Without Writing 28 Lines of SQL)
Performance of Like '%Query%' VS Full Text Search Contains Query
How to Drop a Default Value or Similar Constraint in T-Sql
Union the Results of Multiple Stored Procedures
Grant Privileges for a Particular Database in Postgresql
Copy Data from One Column to Other Column (Which Is in a Different Table)
How to Select Records Without Duplicate on Just One Field in SQL
Postgresql Join with Array Type with Array Elements Order, How to Implement
How to Delete the Top 1000 Rows from a Table Using SQL Server 2008
Copy Table Structure to New Table in SQLite3
How to Aggregate Over Rolling Time Window with Groups in Spark