Convert Excel "If" function to T-SQL query - sql-server

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

Related

IF condition in where clause in SQL Server

I need to write the if condition in the where clause of SQL where I wanted to define that if one column is null then search in another column in a row. I have used union but it's making the query slow to execute, so help me write this statement in the proper way.
This is the code I have right now:
SELECT *
FROM ACCOUNT
WHERE (IF ACCOUNTID IS NULL THEN REF_ACC_ID = 12 ELSE ACCOUNTID = 12)
you can use isnull built in function in sql server
like below link
w3schools
you can write this code for solve your problem
SELECT *
FROM ACCOUNT
WHERE isnull(ACCOUNTID ,REF_ACC_ID ) = 12

SQL Server Determining Hard Coded Date as Larger When It's Not?

An old employee left a massive query behind that I've been debugging and it appears that the issue has come down to SQL Server itself determining a comparison differently than what I would have expected.
I have a table with a column col1 containing the value 20191215 as a datetime.
The part in question is similar to the following:
select case when col1 > '01/01/2020' then 1 else 0 end
This statement is returning 1, suggesting that '12/15/2019' is larger than '01/01/2020'.
I do not need assistance correcting the query, as I have already made changes to do so other than using the comparison the previous employee was using, I am simply curious as to why SQL Server would evaluate this as I have described.
I understand that this is not the typically way SQL Server would store dates as well, would the issue simply be the formatting of the dates?
Current SQL Server version is: SQL Server 2014 SP3 CU3.
SQL Fiddle link that shows the same results
Please note that the link does not contain an exact replica of my case
Edit: Included additional info relevant to actual query.
It is a string comparison not a date comparison:
select case when '12/15/2019' > '01/01/2020' then 1 else 0 end
vs
select case when CAST('12/15/2019' AS DATE) > CAST('01/01/2020' AS DATE) then 1 else 0 end
db<>fiddle demo
I am simply curious as to why SQL Server would evaluate this as I have described.
'12/15/2019' it is a string literal, SQL Server does not know you want to treat a date unless you explicitly express your intention.
I have a table with a column col1 containing the value 20191216
If you are comparing with a column then the data type of column matters and data type precedence rules

how to convert julian date(csv file) to normal date in sql server

We have tried concatenate data in excel so as to make the insert query, but the date col got converted from 28/04/2017 to '42853'(text format) and when we inserted the data onto sql server it comes in text format.
Name Date
Neha 28/04/2017
but wh
Adding the number from excel as days to the date 1899-12-30 will return a proper date.
select dateadd(day,42853,'18991230')
returns: 2017-04-28
In SQL Server 2012+ you can use try_cast() to convert your text value to an integer.
select
...
, dateadd(day,try_cast(col as int),'18991230')
from t
Prior to that, you can use patindex() to in a case expression to confirm that the column is only numbers before attempting to cast() if there is a chance that some columns in the text column are not numbers.
select
...
, case when patindex('[0-9][0-9][0-9][0-9][0-9]',col) = 1
then dateadd(day,cast(col as int),'18991230')
else null
end
from t

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

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

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

Resources