How to convert an integer column to varchar in SQL Server 2014? - sql-server

I have copied the results from SQL Server except the last column (expected_rn_af) which was added later.
How can I convert column rn_af to expected_rn_af using update statement in SQL Server?

Add 64+rn_af within char()
Update YourTable
Set Expected_Rn_af = char(64+rn_af)

Looks like you want a case statement...
...
case
when rn_af = 1 then 'A'
when rn_af = 2 then 'B'
...
end as expected_rn_af

Related

SQL Server 2008 R2: Query MS Access from SQL Server

Want to retrieve records from Access through linked server in SQL Server and need to convert/cast the column with VARCHAR for some constraint.
My attempt:
SELECT Cola, Colb
FROM MSAccess_LinkedServer...TableName
WHERE CAST([Cola] AS VARCHAR) = 'A123'
Unable to get the result from above query.
But when I remove CAST then I will get the result, but I want to know how to put the cast/convert.
No casting should be needed for text. How about simply:
WHERE [Cola] = 'A123'

COALESCE: SQL Server vs Oracle

I have following script:
SELECT 1
FROM Table t
WHERE COALESCE(NULL, t.ID) = NULL;
t is empty. The query returns 1 for Oracle and it returns nothing for SQL Server.
What is an output of COALESCE operation for SQL Server? Can we fix this code to behave for both DB in the same way?
What's the point of having colaesce here as your first argument is NULL.
Just do this:
SELECT 1
FROM Table t
where t.ID IS NULL;
The problem is not the Coalesce function. If the t table is empty then no rows will be found and returned by SQL Server.

How to convert Oracle to SQL Server

I have the following query that I wanted to convert to in SQL Server but not sure how I should go about this. Should I use a CASE statement because I don't see any true/false conditions in this statement. How should I go about this? Thanks!
ORACLE Query:
IIF(CD_ID = '999999',TO_CHAR(CL_ID) || TO_CHAR(CD_SEQ_NO) || '999.99',TO_CHAR(CL_ID) || TO_CHAR(CD_SEQ_NO) || TO_CHAR(CD_ID))
From SQL SERVER 2012+ we can use CONCAT function.
SELECT IIF(CD_ID = '999999',CONCAT(CD_SEQ_NO, '999.99'),CONCAT(CL_ID,CD_SEQ_NO,CD_ID))
FROM YOURTABLE
Concat function not only concatenate the strings but also does implicit conversion when required
Anything less then SQL SERVER 2012 use CASE statements with explicit conversions to varchar
SELECT CASE
WHEN cd_id = '999999' THEN Cast(cd_seq_no AS VARCHAR(50)) + '999.99'
ELSE Cast(cl_id AS VARCHAR(50))+Cast(cd_seq_no AS VARCHAR(50))+Cast(cd_id AS VARCHAR(50))
END
FROM yourtable

Querying Oracle through SQL Server linked server and date values

I am trying to reach some data in a legacy Oracle DB that is attached to a SQL Server as a "linked server". I have an Oracle table and one of the columns is of Date type. Values in this column can be NULL.
In the query I am building I hope to return either just the "date" portion of the column value or an empty string if the value is NULL.
I am currently trying:
CASE
WHEN ACCOUNT.DATE_REVOKED IS NULL
THEN ''
ELSE CONVERT(DATE, ACCOUNT.DATE_REVOKED)
END
This works for values with actual dates. For NULL values what is returned as "1900/01/01". If I do not use the CASE and just return the result of
CONVERT(DATE, ACCOUNT.DATE_REVOKED)
I will get the date portion or the text "NULL", at least in SQL Server Management Studio.
What am I missing?
as the comments have suggested, you cannot use empty string in date column, therefore, why not use this instead:
CASE
WHEN ACCOUNT.DATE_REVOKED IS NULL
THEN NULL
ELSE CONVERT(DATE, ACCOUNT.DATE_REVOKED)
END

Convert Excel "If" function to T-SQL query

I have a table in a SQL Server database and Excel that looks like
this
Using Excel to calculate the value of "D" in column "H" I have used the following formula:
=IF(G2="NULL",100,IF(A2="NULL",((E2-3-F2)/D2),IF(D2="NULL",((B2-3-C2)/A2),IF(((B2-3-C2)/A2)<((E2-3-F2)/D2),((B2-3-C2)/A2),((E2-3-F2)/D2)))))
The formula works fine and if I want to change the value 3 to 3.2 in the formula and update the values in Excel, I just change 3 to 3.2 to make it look like the formula below:
=IF(G2="NULL",100,IF(A2="NULL",((E2-3.2-F2)/D2),IF(D2="NULL",((B2-3.2-C2)/A2),IF(((B2-3.2-C2)/A2)<((E2-3.2-F2)/D2),((B2-3.2-C2)/A2),((E2-3.2-F2)/D2)))))
However I don't know how to convert this Excel formula to a SQL query and I would appreciate if anyone can help me in writing the correct SQL query statement based on the above formula. Thanks in advance.
In SQL SERVER you can use CASE expression to evaluates a list of conditions and returns one of multiple possible result expressions.
SELECT
CASE WHEN G2 IS NULL THEN 100
ELSE
CASE WHEN A2 IS NULL THEN (E2-3.2-F2)/D2
ELSE
CASE WHEN D2 IS NULL THEN (B2-3.2-C2)/A2
ELSE
CASE WHEN (B2-3.2-C2)/A2 < (E2-3.2-F2)/D2 THEN (B2-3.2-C2)/A2
ELSE (E2-3.2-F2)/D2
END
END
END
END AS Test
FROM YourTable

Resources