Changing Jet SQL IsNull to to SQL IsNull Function - tsql

I have a query in MS Access that I am trying to change to SQL view
One of the select statement part is
IIf(IsNull([Book ID]),-1,[Book ID]) AS SubBookID
Unlike in Access T-SQL wants 2 parameters for the IsNull function.
What I need to do is something like
IIf(IsNull([Book ID],true),-1,[Book ID]) AS SubBookID
But we cannot use true like that cause T-SQL thinks that it is a column name

you are going to check if [Book ID] is null or not. If it is null then you are going to return -1 else you are going to return the [Book ID].
To achieve this you need to right it as:
ISNULL([Book ID],-1) AS SubBookID
As you see you do not need the IIF function anymore in this situation.
Read more about ISNULL in T-SQL: https://learn.microsoft.com/en-us/sql/t-sql/functions/isnull-transact-sql?view=sql-server-2017

Related

When using 'Replace' built in function in T-SQL within a Select Query, does the data on the table get modified?

I have the following query
SELECT
[DocID],
[Docunum],
[Comments] = REPLACE(REPLACE([Comments], CHAR(13), ''), CHAR(10), '')
FROM
[Billy].[dbo].[order]
WHERE
DocDate = '2017-12-20 00:00:00.000'
I was wondering if the replace function, actually changes the value in the database? My concern is that this is ERP and I do not want referential integrity problems. I only want to eliminate the carriage separators from the NVARCHAR column to avoid spacing issues while pasting in Excel. I do not want any values changed in the database.
Any feedback would be appreciated. I have searched and did not find anything that answered this specifically. If I missed something please post link for reference if possible.
Actually here you are using replace in Select query so it will not affect your database it will only affect your result which is returned by this query, so here you are safe.

SSRS and PostgreSQL: How do I pass an optional parameter?

I'm using a PostgreSQL ODBC data source in an SSRS report.
From my previous knowledge with MS SQL Server data sources, to pass an optional parameter to a SSRS dataset query, you would do this:
SELECT *
FROM MyTable
WHERE SomeField = #Param1 OR #Param1 IS NULL
The key was using OR #Param1 IS NULL which basically bypasses the SomeField = #Param1 part and effectively acts as an "optional" filter. However, this doesn't seem to work when using a PostgreSQL ODBC data source (not sure if it's due to Postgres or ODBC or both). I get an error when clicking on Refresh Fields, or when executing/previewing the report.
This might be partly due to not being able to pass named parameters into the SQL, but instead having to use "?" like so:
SELECT *
FROM MyTable
WHERE SomeField = ?
...which makes it impossible to do OR ? IS NULL basically.
So, anybody know how I might be able to pass optional parameters to a PostgreSQL data set in SSRS?
Not a specific ODBC solution but a workaround. This will work if SomeField can not be null:
select *
from mytable
where somefield = coalesce(?, somefield)
If SomeField can be null and you want to return nulls:
where somefield is not distinct from coalesce(?, somefield)
Check is distinct from

t-sql conditional date processing

I have a stored procedure which returns data used in a report. There are numerous paramters the user can specify, two of which are a start and end date. I can use a WHERE create_date BETWEEN #arg_start AND #arg_end statement to filter on the dates.
What I dont know how to do is to handle the situation where the user doesnt supply any dates. CASE doesnt support anything like WHERE create_date = CASE #arg_start WHEN NULL THEN create_date ELSE BETWEEN #arg_start AND #arg_end.
I've done a lot of research on google, msdn, and here and I haven't find out to handle conditional null datetime processing. Although I know its bad form, I can pass programatically pass in a magic date, such as 1/1/1900, to test instead of a null, but that doesn't really help in conditional date processing.
Make it a multi-part condition:
WHERE
(#argstart IS NULL AND #argend IS NULL)
OR (#argend IS NULL AND create_date > #Argstart)
OR (#argstart IS NULL AND create_date < #argend)
OR (Createdate BETWEEN #Argstart AND #Argend)
Something like this:
where (create_date >= #arg_start or #arg_start is null) and
(create_date <= #arg_end or #arg_end is null)
If you are on SQL Server 2008 you should use OPTION (RECOMPILE). Ref: Dynamic Search Conditions in T-SQL
Version for SQL 2008 (SP1 CU5 and later).
Otherwise you might be better of using the answer provided by JNK.
I would make the default values for the stored procedure parameters the "magic dates":
CREATE PROC usp_report
#StartDate datetime = '1900-01-01',
#EndDate datetime = '9999-12-31'
AS
SELECT *
FROM MyTable
WHERE DateField BETWEEN #Startdate AND #EndDate
In your application, if the user has not specified a date, don't pass that parameter to the stored proc.
This would keep your application code clean, and give you the data you need.

Null substitution in SQLite:

In Sybase and MSSqlServer TransactSQL, we have a function IsNull(columnName,valueForNull) to return a typed default value when a column value is null. How can I replicate this functionality in SQLite?
Example in TransactSQL:
select IsNull(MyColumn,-1) as MyColumn from MyTable
If MyColumn is null, this expression will return -1 as the value of MyColumn. Want to do something similar in SQLite.
(Yes, I know I can write my own wrapper function to handle this - that's not the answer I'm looking for)
TIA
You should use the standard COALESCE function:
select coalesce(MyColumn, -1) as MyColumn from MyTable
Any database that understands standard ANSI SQL will support COALESCE so using it is a good habit to get into.
You can use ifnull:
select ifnull(MyColumn,-1) as MyColumn from MyTable
The SQL standard way to do this is with CASE:
SELECT CASE WHEN MyColumn IS NULL THEN -1 ELSE MyColumn END FROM MyTable
Considerably more verbose than engine-specific IFNULL, ISNULL, NZ functions, but more portable.
(Or, as mu points out, you can use the much better COALESCE for this).
SQLite has the IFNULL() built-in function which would do what I think you're trying here.

Stored Procedure vs User Defined Function for Error Handling

My ERP database uses non-nullable datetime fields. However, it enters '' for the datetime when one isn't available and returns ‘1900-01-01 00: 00: 00.000’ as the value.
I want to suppress the 1900 dates while stripping the Date only from the Datetime field. I created the following UDF to do that:
CREATE FUNCTION ExtractDate(#DirtyDate DATETIME)
RETURNS VARCHAR(10) AS
BEGIN
DECLARE #CleanDate VARCHAR(10)
SELECT #CleanDate =
CASE
WHEN #DirtyDate = '' THEN ''
ELSE CONVERT(VARCHAR(10), #DirtyDate, 101)
END
RETURN #CleanDate
END
This works, but I wanted to add error handling in case a user used it on something other than a datetime field. After some Googling, I found out that this isn't possible with UDF.
However, if I write this as a stored procedure, would I still be able to call it in a select statement? Can someone point me in the right direction?
No, you can't call a stored proc in a select statement.
I applaud your ambition in wanting to include error handling, but your best bet is to check for that on the app side - don't allow people to use the function on non-date fields.