Monday, August 21, 2017

Get first name and initial of last name

Sometimes names are longer than convenient for displaying in tables and graphs, so you may consider making them shorter.

Considering the name "Jonathan White" as example, here are a few tips for you:

Only first name

SELECT LEFT(Name, CHARINDEX(' ', Name) - 1)
"Jonathan"

First name plus initial of last name

SELECT LEFT(Name, CHARINDEX(' ', Name) + 1)
"Jonathan W"

Just initials

SELECT  SUBSTRING(Name, 1, 1) + SUBSTRING(Name, CHARINDEX(' ', Name) + 1, 1)
"JW"

Notes

There are some caveats with the above suggestions. They may not work as expected if:
  • If there is only one name, e.g. "Peter"
  • If there is more than 2 names, e.g. "Jennifer Alexandra Smith"
For more advanced processing a UDF (function) or SP (procedure) is probably better adviced.

Wednesday, October 14, 2015

Day of week calculation

With SQL Server you can get the day in week using this simple code:

SELECT DATEPART(weekday, getdate())
This gives you a number from 1 to 7, corresponding to the day in the week.

But which day is the first day in the week?

If you are European - it's Monday.
If you are American - it's Sunday.
If you are SQL Server - it depends...

There's a setting in SQL Server that controls this behavior and perhaps it is not set to your liking. And perhaps you don't want to change it and run the risk of side-effects - or perhaps you simply don't have the permissions.

Despair not!

This simple code will allow you to get the day in week independent of SQL Server's settings:

European:
SELECT (DATEDIFF(dd, DATEADD(YEAR, 0, 0), getdate()) + 1) % 7

American:
SELECT (DATEDIFF(dd, DATEADD(YEAR, 0, 0), getdate()) + 2) % 7

Tuesday, March 3, 2015

How to create lookup views for enum types in ERP system

Problem

Quite often we have to include fields in our data model that have no lookup table in the ERP system. They are usually some kind of "Type" and they are sometimes referred to as "Enum" types in the ERP system.

They have their text values stuck somewhere inside the ERP application layer rather than in real database tables.

Also often it is not desirable to create physical tables in ERP database. And even if we could they would be separated from both the ERP system and the data model we are creating, living their own lives so to speak.

So how do we get a lookup table with the text values corresponding to the enum types?

Solution

One possible solution to this is to create a Dynamic View in Modeller and simple create the record synthetically - with no physical tables involved.

All you need is the ERP system to look up the text values you need and then enter them in the Dynamic View.

Example

Let's say we have an enum called "LineType", which has these values:

0: Item
1: Text

We can create a Dynamic View for these values with this SQL:

SELECT 0 AS LineType_ID, 'Item' AS LineType_Name
UNION ALL
SELECT 1, 'Text'

For practical purposes there will usually be more than 2 values of your enum, so just keep adding "UNION ALL... SELECT..." statements for all the values you need.


That's all. Enjoy!

Thursday, February 13, 2014

Antivirus system makes loading of project very slow

If you observe that Acinta Modeller, Datashop og Enterprise Manager take a very long time to connect to and load metadata then the cause may be an installed antivirus system.

This problem was observed at a customer that had Trend Micro antivirus running.

Just connecting to metadata (after entering login) could take several minutes.
Loading the project - well, we had better things to do.

When we turned off Trend Micro antivirus, all worked normal.

 SOLUTION 

  1. Turn off the antivirus system, or the parts hereof that cause the problem, or
  2. Add the Acinta applications to the antivirus system's "White-list"
For more details about Trend Micro and how to add applications to it's white-list, follow this link:

Thursday, December 13, 2012

Installing on SQL Server 2000

If you need to install Acinta Shortcut on a SQL Server 2000 then there are a couple of changes you need to make. They are briefly described below.

Views

  1. "Acinta_Split" not supported. You'll see an error like this: "Syntax error near a". Acinta_Split is used to decipher "Tælleværker" in C5. Table-valued functions are not supported by SQL Server 2000. You have to remove/comment out all SELECT statements using this function. As a result, you'll not be able to see the results of "Tælleværker".
  2. Top N not supported. A couple of places contain sub-queries in the form:
    SELECT TOP 1 <Expr>
    .....
    ORDER BY ....

    This must be rewritten to:
    SELECT MAX(<Expr>)
    .....

    These functions were introduced in SQL Server 2005.
    How to solve: Replace TOP 1 with MAX( ) and remove the ORDER BY clause.
  3. Row_number() and RANK not supported. In one place these functions are used. There is no simple way to express this function in SQL Server 2000 so the simplest solution is to replace the whole expression by "NULL". If you need this function you'll have to rewrite it yourself. However, it is rarely used so don't worry.

Indexes

Indexes in SQL Server 2000 don't allow the INCLUDE columns. You have to rewrite the indexes and put all needed columns in the main index part. Remember there's a limit of 16 fields per index.

In addition, the "WITH" sub-clause specifying index options, is not supported and must be removed.

For example you have to rewrite this index:
CREATE NONCLUSTERED INDEX [ACINTA_IX_ProjTrans] ON [dbo].[PROPOST]
(
[DATASET] ASC,
[DATO] ASC,
[LXBENUMMER] ASC,
[NUMMER] ASC,
[ART] ASC,
[VARENUMMER] ASC,
[LOKATION] ASC,
[MEDARBEJDER] ASC,
[ENHED] ASC,
[BUDGETKODE] ASC,
[TRANSAKTION] ASC,
[VALUTA] ASC,
[PROTYPE] ASC,
[KOPIERET] ASC,
[OVERFXRTLXN] ASC
)
INCLUDE ( [ANTAL],
[KOSTBELXB],
[TEKST],
[BELXBVAL],
[BELXBSTD],
[DRIFTSFXRTANTAL],
[DRIFTSFXRTBELXBVAL],
[DRIFTSFXRTBELXBSTD],
[KURS],
[DRIFTFXRTLXNTILLXGVAL],
[DRIFTFXRTLXNTILLXGSTD]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

to something like this:
CREATE NONCLUSTERED INDEX [ACINTA_IX_ProjTrans] ON [dbo].[PROPOST]
(
[DATASET] ASC,
[DATO] ASC,
[NUMMER] ASC,
[ART] ASC,
[VARENUMMER] ASC,
[LOKATION] ASC,
[MEDARBEJDER] ASC,
[BUDGETKODE] ASC,
[TRANSAKTION] ASC,
[VALUTA] ASC,
[PROTYPE] ASC,
[OVERFXRTLXN] ASC,
[ANTAL],
[KOSTBELXB],
) ON [PRIMARY]
GO

Other considerations

How do I know which SQL Server version it is?

In order til check the version of SQL Server you need to open SQL Server Management Studio (or Enterprise Manager for older versions) and connect to the server.
The internal version number can be read in the red box: 10.
The internal version numbers have this meaning:
8: SQL Server 2000
9: SQL Server 2005
10: SQL Server 2008
11: SQL Server 2012

Beware of "Compatibility mode"!

SQL Server provides a feature called "Compatibility mode" that enables a newer version of SQL Server to pretend it is an older version. The background for this obscurity is of course to allow older applications to work seamless with newer versions of SQL Server that they were not designed for.

Thus, what may at first sight look like e.g. SQL Server 2008 could in fact be an SQL Server 2000. Or actually it is not that simple. When SQL Server runs in compatibility mode for SQL Server 2000 then the following applies:
  1. It does not recognize newer SQL features such as "TOP" and "Row_number( )". Thus, the fix for these features applies.
  2. It will allow you to create table-value functions (Acinta_Split) but it won't let you use them, thus the remedy for this applies.
  3. It still allows you to create indexes with Included columns. So there's no need to rewrite indexes!
One last thing to consider is whether the compatibility mode is actually correct or if it should be set to the actual version. But you have to verify this; if you change the compatibility mode it may cause some applications to malfunction!

Tuesday, July 10, 2012

Installing on C5 with non-standard schema (not 'dbo')

If you need to install Acinta on a C5 that has been installed with a non-standard schema then you need to make some adjustments to the installation procedure as described below.

How do I know that C5 is installed with a non-standard schema?

When you look at the C5 tables in SQL Server Management Studio you will see that all tables have a pre-fix that is different than 'dbo'.
For example, the table "SALESLINE" could look like this:
c5_supervisor.SALESLINE

instead of this:
dbo.SALESLINE

How do I install on a non-standard schema?

To install Acinta on such a C5 you need to follow the normal installation procedure with these extra steps:
  1. Update Index script. Before installing the indexes you must perform a search-and-replace in the index script where you replace all occurrences of 'dbo.' with the actual schema name.
  2. Rename tables in Modeller. When you open the project in Modeller initially, Modeller will start the Rename wizard because it can't find the tables it is looking for. All you have to do is to follow the instructions on the screen. For example Modeller will tell you: "The table dbo.SALESLINE no longer exists. Do you want to replace it?" and then in the list of tables below you simply select the proper replacement table - c5_supervisor.SALESLINE in this example - and click "Rename"
  3. Update Dynamic Views. You have to locate all dynamic views in Modeller's Warehouse section. Right-click on the dynamic view table's header and choose "Edit Dynamic View SQL" from the context menu. Review the SQL and make sure all table references use the correct schema prefix

Wednesday, May 9, 2012

"Syntax error near a" and the Acinta_Split() function

I have come across this error while compiling the view/dynamic view "ACINTA_DV LedgerOfAccountsRelation" in the C5 data model:
"Syntax error near a".

It happens in this line of the view:

    SELECT COUNT(*)
    FROM Acinta_Split(';', a.TXLLEVXRK) x



The error occurs because SQL Server does not recognize the function "Acinta_Split", which is part of the Acinta Shortcut package.

Now, the function was actually compiled and present in the system, so the error seems mysterious at first.
It turns out that the database was configured to have compatibility level 80 = SQL Server 2000.

And since table-based functions were introduced in SQL Server 2005 it is also the natural explanation why SQL Server throws a syntax error at us.

Solution

To get out of this error you have two choices:

  1. Set the compatibility level of the database to at least 90 (SQL Server 2005)
  2. Comment out the parts of the view that make use of the Acinta_Split function. Block comments have the form: "/* <commented code> */
Obviously, you should choose solution 1. whenever possible. But under some circumstances this is not possible. One such circumstance is when you have a C5 version 3.0 solution, because this version of C5 is only compatible with SQL Server 2000; It does not work with later versions of SQL Server.

Applies to

Please note that this error and its solution applies generally and not just to the C5 solution. I.e. it may apply to:
  • Acinta Shortcut for C5
  • Acinta Shortcut for Ax
  • Acinta Shortcut for Nav
  • Acinta Shortcut for XAL
  • Acinta Shortcut for Visma