How to use TO_Char() in SQL Server 2008 - sql-server

I am trying to convert the SQL statement to SQL Server 2008. So how can I do this? Below is my sql statement which I want to convert. I also try to resolve from the Stackoverflow question but I am not succeeding How to use TO_CHAR function functionality
SELECT
C_Debt_Payment.AD_Client_ID,
(CASE WHEN C_Debt_Payment.AD_Client_ID IS NULL THEN ''
ELSE (COALESCE(TO_CHAR(TO_CHAR(COALESCE(TO_CHAR(table1.Name), ''))),'') ) END) AS AD_Client_IDR
Thanks for your reply and comments

TO_CHAR is Oracle. In SQL Server, use CONVERT:
SELECT C_Debt_Payment.AD_Client_ID,
(CASE WHEN C_Debt_Payment.AD_Client_ID IS NULL THEN '' ELSE COALESCE(CONVERT(varchar(20),table1.name),'') END) AS AD_Client_IDR

Related

Alternative to using FORMAT in SQL Server 2008 R2

I created a SELECT using the following in SQL Server 2012:
SELECT
CAST(FORMAT(CONVERT(DATETIME, date_time, 127), 'yyyy-MM-ddTHH:mm:ssZ') AS NVARCHAR(20)) TimeStamp,
FROM myTable
This will result in a date formatted like 2019-03-15T13:25:19Z
How can I achieve the same result using SQL Server 2008 R2 or older?
You can achieve this far more easily by just using CONVERT:
SELECT CONVERT(varchar(19),GETDATE(),127) + 'Z';
As I mentioned in my comment FORMAT is actually an awful function, it performs terribly. I posted an answer to another question earlier today on just how badly it does compared to a CONVERT. Don't just use this expression on your 2008- servers, replace the FORMAT expression on your 2012+ servers with this one too.
I think this does what you want:
select replace(convert(varchar(255), getdate(), 120), ' ', 'T') + 'Z'
Code 127 returns milliseconds, which you do not seem to want, so 120 seems more appropriate.

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

Migrate Oracle SQL and Sql Server on new Date

I want to make a query which is compatible for Oracle SQL and PL/SQL.
I use the Dateadd function in SqlServer, which is incompatible for Oracle, there you can use ADD_MONTHS.
Sql Server:
DATEADD(MONTH, -1, GETDATE())
Oracle
to_date(ADD_MONTHS(SYSDATE,1),'DD-MON-YYYY'))
Is there any ANSI statement to merge these?
As an example, you can create this function -
create or replace function dateadd(date_type varchar2, shift number, date_to_move date) return date is
x date;
begin
if date_type = 'MONTH' then
select to_date(add_months(sysdate,-1),'DD/MM/YYYY')
into x
from dual;
end if;
return x;
end dateadd;
and then the following query will work in oracle -
select dateadd('month',-1,sysdate)
from dual;
Of course then you still need to the same thing for get_date, and you will still have certain differences between both platforms that will probably prevent running the exact same code on Oracle and SQL Server.
All in all, #Boneist solution sounds the fittest to your problem.

Convert oracle date string to SQL Server datetime

In a SQL Server 2000 DB, I have a table which holds string representations of Oracle DB dates. They are formatted like "16-MAY-12". I need to convert these to datetime. I can not seem to find a conversion style number that matches, nor can I find a function that will allow me to specify the input format. Any ideas?
This seems to work for me:
SELECT CONVERT(DATETIME, '16-MAY-12');
You can also try using TO_CHAR() to convert the Oracle values to a more SQL Server-friendly format (best is YYYYMMDD) before pulling them out of the darker side.
Follow Aaron's advice and cast to string on the Oracle side and then did a check/recast on the MS SQL side. See example below:
;WITH SOURCE AS (
SELECT * FROM openquery(lnk,
'SELECT
TO_CHAR(OFFDATE , ''YYYY-MM-DD HH24:MI:SS'') AS OFFDATE,
FROM
ORACLE_SOURCE')),
SOURCE_TRANSFORM AS
(
SELECT
CASE
WHEN ISDATE(OFFDATE) = 1 THEN CAST(OFFDATE AS DATETIME)
ELSE NULL END AS OFFDATE
FROM
SOURCE
)
SELECT * FROM SOURCE_TRANSFORM

Resources