Showing posts with label null. Show all posts
Showing posts with label null. Show all posts

Friday, March 23, 2012

Importing NULL into table SQL Server 2005

I've tried to import a text file into a table using the Import/export wizard.

My problem starts with some columns that should have NULL values (i.e. zero-length string) but the wizard doesn't recognize that it's NULL.

How do I solve the problem?

Thanks,

Mich

First, a zero-length is not NULL. NULL is a special character.

If you want NULLs to be inserted, open up the package and edit it (that the import/export wizard created for you) and add a derived column transformation before going to the destination.

Use an expression similar to this for each of your columns:

[myColumn] == "" ? NULL(DT_WSTR,20) : [myColumn]

I'm going by memory, but I believe that would work. The above assumes myColumn has a length of 20 bytes.

|||

Thank you for your answer.

My knowledge of the import/export wizard is limited.

I would like to know whether the expression should be added in the "Suggest Types" button or elsewhere.

Is the expression related to the destination column names or the columns the Wizard created?

I'd appreciate it if you could give me a step-by-step guide on how to do it, since I found the sql help file very unhelpful.

Thank you again.

Importing Null Date Fields

I'm using SQL Server Express and am trying to import a CVS file. The CVS file contains a string field (named DAS) that represents a Date. This field can be null.

I've tried using the DTS Wizard to import this CVS file and convert the DAS field to a Date, which works great until it hits a record with a NULL DAS field. It then throws a convertion error.

Still using the DTS Wizard, I've changed the DataType of the DAS field in the Source file to [DT_DATE], it works fine but all the null dates are converted to 12/30/1899.

Is there a way (DTS Wizard or something else) that will allow me to import these CVS files with null Date fields and keep them as null in SQL Server table.

Thanks for any help,

Jon

Hi,

I read your post and I can advice you the next:

1. Once you have date 12/30/1899 instead all NULL values in SQL Server => you may easy to sort that records, to mark them and place NULL value with “copy” and “paste”. It is easy and quickly.

2. You may write a program instead DTS Wizard /I don’t know that wizard/. That program will read from your CVS file and write to SQL server. From the program you will have full control on all fields. I personally prefer to write a program when I have some unusual case.

I hope that advices will solve the problem. But if you still can’t make NULL values – let me know.

Regards,

Hristo Markov

|||Off the top of my head, I'm thinking there is an option to "Keep Nulls" inside the wizard. I haven't run it and I'm not on my machine w/ SSIS installed so I can't verify it. If you do see such an option, that is how you tell SSIS to preserve the NULLs.

If this isn't an option, then you'll have to go one step deeper and either do custom transformations or write an EXECUTE SQL TASK that will go through and update any record of 12/30/1899 to be NULL.

Importing MSAccess data into SQL Tables

I am attempting to import data from an MS Access 2000 table into a SQL
Server 2000 table, but NULL values in the Access table are causing the DTS
import to fail.
I thought that if I defined a column in SQL as 'NOT NULL', but assigned a
default value of '', then it would allow an import containing a NULL, which
it would convert to a ''. But I guess I was wrong? Is there is a way to
import MSAccess data containing nulls into a SQL table where the nulls would
be automatically be converted to ''?
Thanks!
Mark
Here is the definition for the SQL table:
CREATE TABLE [dbo].[creditcard] (
[CARD_NAME] [nchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[CARD_ABB] [nchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[ACTIVE] [nchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[creditcard] ADD
CONSTRAINT [DF_creditcard_CARD_NAME] DEFAULT ('') FOR [CARD_NAME],
CONSTRAINT [DF_creditcard_CARD_ABB] DEFAULT ('') FOR [CARD_ABB],
CONSTRAINT [DF_creditcard_ACTIVE] DEFAULT ('') FOR [ACTIVE]
GO
Mark,
make sure that on the options tab of the transform data task, 'check
constraints' is enabled.
HTH,
Paul Ibison
|||I'm sorry but I don't see the option 'check constraints'. I am running the
'Data Transformation Services Import/Export wizard'.
Here are the screens I see:
1) Choose a data source: I choose the access database
2) Choose a destination: I choose the target database
3) Specify Table Copy or Query: I choose 'Copy table(s) and view(s) from the
source database
4) Select Source Tables and Views: I select my source table and target table
here
5) Save, schedule and replicate package: I choose 'Run Immediately'
6) Completing the DTS Import/Export Wizard: I choose 'Finish' to run the
import.
At this point it appears to import a number of records and then I get the
popup indicating that it can not import the record with the NULL value in
the column.
In all of that I'm afraid I could not see a transform data task. I hate to
be dense, but could you elaborate on where I should look for that?
Many thanks!
Mark
"Paul Ibison" <Paul.Ibison@.Pygmalion.Com> wrote in message
news:eNw%23ZyPQEHA.3708@.TK2MSFTNGP10.phx.gbl...
> Mark,
> make sure that on the options tab of the transform data task, 'check
> constraints' is enabled.
> HTH,
> Paul Ibison
>
|||DTS can only carry over constraint if you go between SQL Servers. I think that Access comes with some upsizing
wizard, you might want to check in an Access groups.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Mark Findlay" <mfindlay@.speakeasy.org> wrote in message news:%238kPFkQQEHA.2876@.TK2MSFTNGP09.phx.gbl...
> I'm sorry but I don't see the option 'check constraints'. I am running the
> 'Data Transformation Services Import/Export wizard'.
> Here are the screens I see:
> 1) Choose a data source: I choose the access database
> 2) Choose a destination: I choose the target database
> 3) Specify Table Copy or Query: I choose 'Copy table(s) and view(s) from the
> source database
> 4) Select Source Tables and Views: I select my source table and target table
> here
> 5) Save, schedule and replicate package: I choose 'Run Immediately'
> 6) Completing the DTS Import/Export Wizard: I choose 'Finish' to run the
> import.
> At this point it appears to import a number of records and then I get the
> popup indicating that it can not import the record with the NULL value in
> the column.
> In all of that I'm afraid I could not see a transform data task. I hate to
> be dense, but could you elaborate on where I should look for that?
> Many thanks!
> Mark
> "Paul Ibison" <Paul.Ibison@.Pygmalion.Com> wrote in message
> news:eNw%23ZyPQEHA.3708@.TK2MSFTNGP10.phx.gbl...
>
|||Mark,
the package uses a data transform task in the background which has loads of
properties that are not exposed in the wizard, so the best thing to do is to
save the package without running it. After that, open it and double-click
the data transform task. On the final tab, you'll see the option I'm talking
about. The check constraints option should initiate the default's use.
HTH,
Paul Ibison

Wednesday, March 21, 2012

Importing from Excel

I am having a problem with DTS importing NULL values from an Excel spreadsheet when there are numbers and text in the same column. Other than that, the import works fine. I am using VBScript in an ASP web page to create and execute a DTS package to import an Excel file.

I have read Allan Mitchell's article about using IMEX=1 (http://www.sqldts.com/default.aspx?254). I added IMEX=1 and modified the registry setting, but when I do that NO data at all is imported.

Any ideas? My code is very long, so I'll only post it if you'd really like to see it.

Much thanks in advance!

BillI would export the excel file into a delimited file - the ms driver for excel causes too many headaches. Also, did you modify the TypeGuessRows setting in your registry ?|||Originally posted by rnealejr
I would export the excel file into a delimited file - the ms driver for excel causes too many headaches. Also, did you modify the TypeGuessRows setting in your registry ?

I did modify the TypeGuessRows setting to 0. Is there a way to programmatically export the Excel file to a CSV file?

Thanks,

Bill|||It has been a while since I used the TypeGuessRows - but I believe setting it to 0 will not work - you have to explicitly supply a value (something big enough to sample your data).

Programmatically - Do you mean from within sql server or any executable ?|||Originally posted by rnealejr
It has been a while since I used the TypeGuessRows - but I believe setting it to 0 will not work - you have to explicitly supply a value (something big enough to sample your data).

Programmatically - Do you mean from within sql server or any executable ?

I read somewhere to set it to 0 and use IMEX=1. I just tried setting it to 16 and 8, but neither of those worked either.

Actually, when I said programatically I meant VBScript, but I guess that's for the VBScript forum!

Thanks,

Bill|||You can create an excel object - open the workbook - use the saveas method for the workbook.|||Originally posted by rnealejr
You can create an excel object - open the workbook - use the saveas method for the workbook.

Where can I find information on using the excel object's saveas function? I'm sure there are a number of options, and I'd like to take a look at them.

Thanks!

Bill|||Here is the snapshot from the help (Also, look for a vbaxl9.chm file on your computer):

Saves changes to the sheet (Syntax 1) or workbook (Syntax 2) in a different file.

Syntax 1

expression.SaveAs(Filename, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AddToMru, TextCodePage, TextVisualLayout)

Syntax 2

expression.SaveAs(Filename, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AccessMode, ConflictResolution, AddToMru, TextCodePage, TextVisualLayout)

expression Required. An expression that returns a Chart or Worksheet object (Syntax 1) or a Workbook object (Syntax 2).

Filename Optional Variant. A string that indicates the name of the file to be saved. You can include a full path; if you dont, Microsoft Excel saves the file in the current folder.

FileFormat Optional Variant. The file format to use when you save the file. For a list of valid choices, see the FileFormat property. For an existing file, the default format is the last file format specified; for a new file, the default is the format of the version of Excel being used.

Password Optional Variant. A case-sensitive string (no more than 15 characters) that indicates the protection password to be given to the file.

WriteResPassword Optional Variant. A string that indicates the write-reservation password for this file. If a file is saved with the password and the password isnt supplied when the file is opened, the file is opened as read-only.

ReadOnlyRecommended Optional Variant. True to display a message when the file is opened, recommending that the file be opened as read-only.

CreateBackup Optional Variant. True to create a backup file.

AccessMode Optional Variant. The workbook access mode. Can be one of the following XlSaveAsAccessMode constants: xlShared (shared list), xlExclusive (exclusive mode), or xlNoChange (dont change the access mode). If this argument is omitted, the access mode isnt changed. This argument is ignored if you save a shared list without changing the file name. To change the access mode, use the ExclusiveAccess method.

ConflictResolution Optional Variant. Specifies the way change conflicts are resolved if the workbook is a shared list. Can be one of the following XlSaveConflictResolution constants: xlUserResolution (display the conflict-resolution dialog box), xlLocalSessionChanges (automatically accept the local users changes), or xlOtherSessionChanges (accept other changes instead of the local users changes). If this argument is omitted, the conflict-resolution dialog box is displayed.

AddToMru Optional Variant. True to add this workbook to the list of recently used files. The default value is False.

TextCodePage Optional Variant. Not used in U.S. English Microsoft Excel.

TextVisualLayout Optional Variant. Not used in U.S. English Microsoft Excel.

The following are the available file formats:

xlAddIn
xlCSV

xlCSVMac

xlCSVMSDOS

xlCSVWindows

xlCurrentPlatformText

xlDBF2

xlDBF3

xlDBF4

xlDIF

xlExcel2

xlExcel2FarEast

xlExcel3

xlExcel4

xlExcel4Workbook

xlExcel5

xlExcel7

xlExcel9795

xlHTML

xlIntlAddIn

xlIntlMacro
xlSYLK
xlTemplate

xlTextMac

xlTextMSDOS

xlTextPrinter

xlTextWindows

xlUnicodeText

xlWJ2WD1

xlWK1

xlWK1ALL

xlWK1FMT

xlWK3

xlWK4

xlWK3FM3

xlWKS

xlWorkbookNormal

xlWorks2FarEast

xlWQ1

xlWJ3

xlWJ3FJ3|||Awesome, thanks!

So if I do this:

<code>
set xlApp = CreateObject("excel.application")
xlApp.Workbooks.Open <filename>
xlApp.ActiveWorkbook.SaveAs <newFileName>, xlCSVWindows
xlApp.ActiveWorkbook.Close
xlApp.Quit
Set xlApp = Nothing
</code>

It would save it as a CSV file?

Thanks!

Bill|||Yes, that looks good. Let me know if there are any problems.|||Originally posted by rnealejr
Yes, that looks good. Let me know if there are any problems.

Thanks, I'm going to try to give it a test today!|||Originally posted by rnealejr
Yes, that looks good. Let me know if there are any problems.

Okay, I tried that code and I'm getting the following error message:

Microsoft Excel error '800a03ec'
SaveAs method of Workbook class failed

Any ideas?

Thanks!

Bill|||Originally posted by rnealejr
Yes, that looks good. Let me know if there are any problems.

Okay, I may have found the issue, but I'm not sure. I saved the current workbook file format value to a variable and printed it to the screen. The value was -4143. Is the fileformat value supposed to be a number or the values from the help file (i.e., xlCSV)?

Thanks for all your help!

Bill|||I do not have to vb at the moment to confirm but the xlCSV value should be and the xlWindowsCSV is 23.|||Also, sorry for the delay - your post got buried.|||Originally posted by rnealejr
I do not have to vb at the moment to confirm but the xlCSV value should be and the xlWindowsCSV is 23. \

No problem on the delay, I really appreciate the help - is there a list of these values anywhere? I've been looking, but I must not be using the right terminology.

Thanks!

Bill|||This link might be helpful:

link (http://techsupt.windowware.com/TS/T000001033005F9.html)

Monday, March 12, 2012

Importing Date Values from a flat file into the database

Hi

I am trying to to import a flat file into a table in my database, i get all the values right except for the date, it keeps on inserting NULL values into the date fields.

The date format in the flat file is '20070708' etc.

Does anyone know what i can do to fix this?

I've tried to change the datatype values that it imports, but it still ignores it and inserts NULL values

Any help will be greatly appreciated

Kind Regards

Carel Greaves

Search this forum for "YYYYMMDD" and you'll find your answer along with examples.

Basically, you need to substring the date field into the various date parts and then assemble a date in the format of perhaps mm/dd/yyyy before converting into a datetime field.|||

Thanks

Sunday, February 19, 2012

Importing a null delimited file

I have a text file that is delimited by nulls. Any idea on the best way to get this into a SQL Server table?You can't delimit with NULLs. Do you mean space characters: " "?|||What do you mean 'null deliminated' file. Technically all files are deliminated by a null character. So I think I misinterpret your meaning.|||Do you mean CHAR(0)?|||"Technically all files are deliminated by a null character."?

...well, I guess you could could say that every text file is delimited by an infinite number of NULL values...|||no, not an infinite number, the sequence stops after the largest prime ;)

actually, i think robert nailed it -- it's probably a hex 00 (also known as a NUL)

see http://www.jimprice.com/jim-asc.htm#controlcodes|||try using \0|||kaiowas, this is sql server, where would you suggest to use that backslash?|||r937, he has a text-file so I figured he's trying to use bcp to get the file into a table.|||Good idea using bcp. I did:

BULK INSERT nullfile FROM 'C:\nullfile.txt'
WITH (
DATAFILETYPE = 'char',
FIELDTERMINATOR = '\0',
ROWTERMINATOR = '\n'
)

and it worked :D