•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 375,232 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,043 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 9130 | Replies: 15 | Solved
![]() |
•
•
Join Date: Sep 2006
Location: San Diego, CA USA
Posts: 92
Reputation:
Rep Power: 2
Solved Threads: 2
Ok. One more thing (and I use this technique a lot). You can conditionally inject a zero to your string data based on conditions.
This code will inject a '0' in front of any single digit number.
SQL Syntax (Toggle Plain Text)
case when ((cast(f1 AS int) > -10) OR (cast(f1 AS int) < 10)) then '0' + f1 else f1 end
This code will inject a '0' in front of any single digit number.
-Mike
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 1
i am changing the excel column to varchar before i actually write to the sql table.
i used this earlier:
INSERT INTO table(c1,c2)
SELECT Code1,Desc
FROM OPENDATASOURCE
('Microsoft.Jet.OLEDB.4.0','Data Source="C:\test1.xls";Extended Properties="Excel 8.0;IMEX=1;HDR=YES"')
...Sheet1$
I modified the above as follows and i think i see correct results:
INSERT INTO table(c1,c2)
SELECT CAST(Code1 AS varchar(6)),Desc
FROM OPENDATASOURCE
('Microsoft.Jet.OLEDB.4.0','Data Source="C:\test1.xls";Extended Properties="Excel 8.0;IMEX=1;HDR=YES"')
...Sheet1$
i have to do vast testing before i can say that this worked.
i used this earlier:
INSERT INTO table(c1,c2)
SELECT Code1,Desc
FROM OPENDATASOURCE
('Microsoft.Jet.OLEDB.4.0','Data Source="C:\test1.xls";Extended Properties="Excel 8.0;IMEX=1;HDR=YES"')
...Sheet1$
I modified the above as follows and i think i see correct results:
INSERT INTO table(c1,c2)
SELECT CAST(Code1 AS varchar(6)),Desc
FROM OPENDATASOURCE
('Microsoft.Jet.OLEDB.4.0','Data Source="C:\test1.xls";Extended Properties="Excel 8.0;IMEX=1;HDR=YES"')
...Sheet1$
i have to do vast testing before i can say that this worked.
•
•
Join Date: Sep 2006
Location: San Diego, CA USA
Posts: 92
Reputation:
Rep Power: 2
Solved Threads: 2
Be careful in situations where you have different data types in the same column.
For example, in my spreadsheet, the first four rows contain information that really has nothing to do with the data. The real data starts from the fifth row and down. So in my example, I have integer data in the first column (starting from the fifth row). However, cell A1 contains a title that is a string. If I use
In your case, you're casting to a varchar so no big deal, but if you are casting to a numeric data type you'll need to be aware of the pit falls.
Bottom line - lots of error checking and data massaging may be required.
For example, in my spreadsheet, the first four rows contain information that really has nothing to do with the data. The real data starts from the fifth row and down. So in my example, I have integer data in the first column (starting from the fifth row). However, cell A1 contains a title that is a string. If I use
cast(f1 as int) in my code, it will fail on cell A1.In your case, you're casting to a varchar so no big deal, but if you are casting to a numeric data type you'll need to be aware of the pit falls.
Bottom line - lots of error checking and data massaging may be required.
-Mike
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 1
Higa, you are right. But in my case, the excel sheet will have only two columns and I know for sure it doesnt cause any problems.
Now I have a new problem. Its about sheet names in the excel. I dont have the problem and solution clearly yet. I will post both of them soon.
OK..here it is. I am trying to read a specific sheet from an excel file. I realized that the name of the sheet may not be same all the time, but the position of the sheet is fixed. I can take the sheet name as input, but sometimes it may have spaces in that name and i tried in vain to read those sheets with spaces. So, two ways..one is to be able to access the sheets by their postion or to be able to read the sheet names with spaces.
If anyone know how to tackle this problem, please post a solution.
Now I have a new problem. Its about sheet names in the excel. I dont have the problem and solution clearly yet. I will post both of them soon.
OK..here it is. I am trying to read a specific sheet from an excel file. I realized that the name of the sheet may not be same all the time, but the position of the sheet is fixed. I can take the sheet name as input, but sometimes it may have spaces in that name and i tried in vain to read those sheets with spaces. So, two ways..one is to be able to access the sheets by their postion or to be able to read the sheet names with spaces.
If anyone know how to tackle this problem, please post a solution.
Last edited by tatini : Nov 2nd, 2007 at 6:27 pm.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb MS SQL Marketplace
•
•
•
•
access ajax asp breach broadband code combo crime daniweb data data protection data transfer database developer development drive dropdownlist forensics government hacker hard hardware hitachi internet linux microsoft module msdn net news office reuse security software sql storage survey terabyte vista web wikipedia
- Manipulate Excel file using C++?? (C++)
- Getting A Data From Ms Eccess And Putting It In An Existing Excel File Using Vb 6.0 (Visual Basic 4 / 5 / 6)
- getting data from a text file and putting it in an excel file using visual basic 6.0 (Visual Basic 4 / 5 / 6)
Other Threads in the MS SQL Forum
- Previous Thread: Odd issue with LIKE operator
- Next Thread: Identity column


Linear Mode