Wednesday, February 26, 2014

Getting the first date of the month in SQL without any functions

One of the date columns in my database is an integer as it is a dimension key
I was trying to get some results grouped for the whole month by this date integer column.

This is how I went about getting it in an easier manner without using any DATEADD and DATDIFF functions

Let us say that the date column name is dim_date_key which is an integer data type

So I used the following SQL code to get the sum of the amount for the month

select sum(amount), dim_date_key/100*100 + 1 month_start_Date from table_name
where dim_date_key/100*100 + 1 >= 20130401
group by dim_date_key/100*100 + 1

Here if we just look at the expression dim_process_date_key/100*100 +1 in mathematical terms it is confusing as to how this can get the start of the month.

But the key here is the integer data type of the date column.


  • When the expression is evaluated, the expression dim_process_date_key/100 gets evaluated first.  This gives the answer as 201304 since the data type is an integer.
  • Then the expression 201304 * 100 is evaluated which yeilds the result as 20130400 which is an integer.  
  • Then the expression 20130400 + 1 is evaluated which yeilds the start date of the month which is 20130401


Wednesday, January 15, 2014

Import/Export data wizard in MSSQL 2008 R2 Errors -- Resolved

Today I was using the import export wizard to import data from one of the database in the production system to the database in the development system.

To do this -- Right click on the database (in the development system) choose -- tasks -- Import data
Follow the steps in the data wizard for choosing the source and destination tables.  I used the write a query to copy the data option.

Checked the mappings
and clicked Finish

I expected the wizard to run without any problems.  But................ the following errors were thrown.

- Copying to [dbo].[FACT_ACCOUNT_TRANSACTION] (Error)
Messages
Error 0xc0202009: Data Flow Task 1: SSIS Error Code DTS_E_OLEDBERROR.  An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available.  Source: "Microsoft SQL Server Native Client 10.0"  Hresult: 0x80004005  Description: "The INSERT permission was denied on the object 'FACT_ACCOUNT_TRANSACTION', database 'BIW', schema 'dbo'.".
 (SQL Server Import and Export Wizard)

Error 0xc0209029: Data Flow Task 1: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR.  The "input "Destination Input" (128)" failed because error code 0xC020907B occurred, and the error row disposition on "input "Destination Input" (128)" specifies failure on error. An error occurred on the specified object of the specified component.  There may be error messages posted before this with more information about the failure.
 (SQL Server Import and Export Wizard)

Error 0xc0047022: Data Flow Task 1: SSIS Error Code DTS_E_PROCESSINPUTFAILED.  The ProcessInput method on component "Destination - FACT_ACCOUNT_TRANSACTION" (115) failed with error code 0xC0209029 while processing input "Destination Input" (128). The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running.  There may be error messages posted before this with more information about the failure.
 (SQL Server Import and Export Wizard)

Error 0xc02020c4: Data Flow Task 1: The attempt to add a row to the Data Flow task buffer failed with error code 0xC0047020.
 (SQL Server Import and Export Wizard)

Error 0xc0047038: Data Flow Task 1: SSIS Error Code DTS_E_PRIMEOUTPUTFAILED.  The PrimeOutput method on component "Source - Query" (1) returned error code 0xC02020C4.  The component returned a failure code when the pipeline engine called PrimeOutput(). The meaning of the failure code is defined by the component, but the error is fatal and the pipeline stopped executing.  There may be error messages posted before this with more information about the failure.
 (SQL Server Import and Export Wizard)



Finally I resolved this error by dropping the column store index on the table and rerunning the wizard.

And then recreated the column store index after the data has been copied.

Monday, December 23, 2013

Work Directory in ODBC Connection is empty

 "Work Directory in ODBC Connection is empty, unable to proceed."

 That was the error I was getting today when I was trying to load a table from MDS using Wherescape Red. At first I didnot understand why there is a work directory needed. When I looked at the MDS cnnection settings The default Load was set to Native ODBC and the work directory was not set as shown below.

 

 As soon as I have set the directory to c:\ the loading of the table worked.

 Because the Native ODBC load reads all the data and creates temporary tables, this method of data loading requires a work directory to be specified so that the data is written in batch mode rather than one row at a time similar to ODBC load. When the reading a writing takes place one row at a time we need not specifiy the work directory. But the native ODBC load is faster because of a batch write process.

Ensure that a process for deleting the temporary tables is in place if you are loading using Native ODBC.

If you are using the ODBC Load there is no need to specify the work directory.

Tuesday, August 20, 2013

ERROR 22-322: Syntax Error is SAS code

I have been in hibernation for too long now. I have changed jobs and now hjaving an opportunity to work on SAS code after nearly 11 years. Today I was running a SAS program and got the following error.

ERROR 22-322: Syntax error, expecting one of the following: a name, -, :, FORMAT, INFORMAT, LABEL, LABEL, LENGTH,

To resolve this as the error suggested there was a syntax error in my code where I was not using the proper syntax in the ATTRIB statement I was using

Name label 'Employee Name';

Here the label statement needs an '=' so when I change this to the following statement

Name label = 'Employee Name' ;

the sas code was executed successfully.

Thursday, June 20, 2013

Free ebooks from Microsoft

Here is a link to a huge collection of free ebooks posted by Eric Ligman from Microsoft.  Hope you will find these books useful in your career development.

Monday, February 04, 2013

I thought I will start updating my blog after a bit hibernation.


So here is a tip for getting rid of the blank page that appears if you use a Next Page After setting in the group section as shown below.


Use the formula Not Onlastrecord


To do this click on the formula section against the Next PAge After setting and type in Not Onlastrecord

Save the report and the last blank page will disappear.

Friday, July 13, 2012

Sql Server Error 3154

I created a new database today and was wanting to restore this new database from another exisitng database using the restore option available in the wizard.


But I got the 3154 error as below.

So I had to restore to the restore database command in the SSMS. So here is the first option I tried

restore database databasename
from disk 'filepath.bak'
with replace

but this gave an error because the database and log files were in use for the exisitng database and had to use the with move command as follows.

restore database databasename
from disk 'filepath.bak'
with move data file to 'file path',
move log file to 'file path',
replace



The database was restored successfully.