Hello I am trying to convert a MSAccess Query to TSQL.
EDIT FOR ADDITIONAL DETAIL;
A user created an MS Access application using my database as the source tables via ODBC. Per our policy, MS Access is only to be used for adhoc analysis, no storage of data. Well this use created a bunch of new tables in his application and is using it as production data. I have already replicated his logic and moved it to SQL Server via triggered scripts. I am now trying to populate his historical data into SQL.
In my db MATERIAL_NUMBER is a varchar(30) data type. We have all sorts of different conventions from acquisitions.
One of the companies we acquired has PNs such as "000000000000000001". The creator of the Access application did not like that the leading zeros would prompt excel to say "number stored as text" when exported and so in his tables he applied the following to the material number to store "000000000000000001" as "1"
This is the Access Query.
IIf(IsNumeric([MATERIAL_NUMBER])=0,[MATERIAL_NUMBER],CStr(CDbl([MATERIAL_NUMBER]))) AS PN_FORMAT
The issue now is that I can not join his tables to mine as he has broken MATERIAL_NUMBER. What I am trying to do is reverse engineer his conversion so that I can update the values back to the standard convention.
This is what I tried in SQL
CASE WHEN ISNUMERIC([MATERIAL_NUMBER])=1
THEN CONVERT(VARCHAR(30),CAST([MATERIAL_NUMBER] AS BIGINT))
ELSE [MATERIAL_NUMBER]
END AS PN_FORMAT
I get the following Error
Msg 8114, Level 16, State 5, Line 3 Error converting data type varchar to bigint.
Here is the full Query
TRUNCATE TABLE PN_FORMAT;
INSERT INTO PN_FORMAT
SELECT O.BASE_PART_CODE, O.MATERIAL_NUMBER,
CASE WHEN ISNUMERIC([MATERIAL_NUMBER])=1
THEN CONVERT(VARCHAR(30),CAST([MATERIAL_NUMBER] AS BIGINT))
ELSE [MATERIAL_NUMBER]
END AS PN_FORMAT
FROM [DCA-DB-326\MSBI_RS].[AGS_DATAMART].dbo.OPR_MATERIAL_DIM O
EDIT FOR UPDATED ATTEMPT; As per suggestion, I tried this...
TRUNCATE TABLE TEMP_PN_FORMAT;
INSERT INTO TEMP_PN_FORMAT
SELECT O.BASE_PART_CODE,
O.MATERIAL_NUMBER,
CASE WHEN ISNUMERIC([MATERIAL_NUMBER])=1 THEN CONVERT(VARCHAR(30),CAST([MATERIAL_NUMBER] AS FLOAT))
ELSE [MATERIAL_NUMBER] END AS PN_FORMAT
FROM [DCA-DB-326\MSBI_RS].[AGS_DATAMART].dbo.OPR_MATERIAL_DIM O
No error this time, but only smaller numbers are working. 647 worked as expected and was able to join, 4000192 did not.