Here was the Java program.
The program scans this file (change the name to the code below) to
CAST(0x... AS DateTime)
and replaces them with appropriate
CAST('yyyy-MM-dd HH:mm:ss.SSS' AS DateTime)
.
For example, given that SELECT CAST (0x00009CEF00A25634 as datetime) returns 2009-12-30 09:51:03.000 , the program looks at the file for CAST(0x00009CEF00A25634 AS DateTime) and replaces them with CAST('2009-12-30 09:51:03.000' AS DateTime) .
I used it to convert the generated SQL Server script to an embedded H2 database that I could understand.
Although this worked for me, I suggest you check it out (just run some test data and see) before using the actual data.
import java.io.*; import java.text.*; import java.util.*; import java.util.regex.*; public class ReplaceHexDate { public static void main(String[] args) throws Exception { String inputFile = "C:/input.sql"; String inputEncoding = "UTF-8"; String outputFile = "C:/input-replaced.sql"; String outputEncoding = "UTF-8"; BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), inputEncoding)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), outputEncoding)); String line; while ((line = br.readLine()) != null) { if (line.indexOf("CAST(0x") > -1) { bw.write(replaceHexWithDate(line)); } else { bw.write(line); } bw.newLine(); } br.close(); bw.flush(); bw.close(); } private static String replaceHexWithDate(String sqlLine) throws ParseException { Pattern castPattern = Pattern.compile("(CAST\\()(0x[A-Fa-f0-9]{16})( AS DateTime\\))"); Matcher m = castPattern.matcher(sqlLine); while (m.find()) { String s = m.group(2); sqlLine = sqlLine.replace(s, "'"+sqlServerHexToSqlDate(s)+"'"); } return sqlLine; } public static String sqlServerHexToSqlDate(String hexString) throws ParseException { String hexNumber = hexString.substring(2);
acdcjunior
source share