2011-08-02 60 views
2

我正在將數據從不同源類型(如Oracle,MySQL,Access,Sharepoint等)移動到SQL。將字符串作爲日期時間插入SQL而不使用CAST

SQL腳本是在C#中產生這樣的:

with cte (ID, changedate, SupplierID) 
as (
SELECT 1720, '1997-12-17 12:00:00 AM', 763 
UNION ALL 
SELECT 1721, '1900-01-01 12:00:00 AM', 114) 
select * into components8_2_2011_3 from cte 

的DATATIME領域作爲VARCHAR這是錯誤的產生。

在C#中,我可以看到列類型是datetime,但MSSQL無法將該字符串識別爲datetime。

我將如何去獲取上述腳本創建一個日期時間字段沒有添加一個CAST語句到changeDate字段?

+0

擺脫Ø f結尾處的「AM」 – Pieter888

+0

不是,那不是正確的:SQL仍然將該字段創建爲varchar。 – callisto

回答

2

如何使用 「導行」

with cte (ID, changedate, SupplierID) as (
    --type but no data 
    select 0, getdate(), 0 where 0=1 union 

    SELECT 1720, '1997-12-17 12:00:00 AM', 763 
    UNION ALL 
    SELECT 1721, '1900-01-01 12:00:00 AM', 114 
) 
select * into components8_2_2011_3 from cte 
+0

把它改爲'...從cte哪裏ID <> 0「 –

+0

這讓我很痛苦,我只有一個upvote給:謝謝!! 這正是我尋找的解決方案的類型。 – callisto

2

你不能。除非你告訴它,否則SQL Server不知道它是日期時間。爲什麼要這樣?

例如:

  • 12:00:00 AM是午夜,因此,它是date?或者SQL Server 2005的smalldatetime
  • 你會存儲時間部分?如果是這樣,什麼分辨率(smalldatetime,datetime2等)
  • 這是否包括任何時區信息?
  • 它是UTC還是當地時間?

如果你不想CAST(或轉換),然後創建表components8_2_2011_3第一

編輯:亞歷克斯·K公司的招很聰明,但是你想在接下來的100萬行全datetimedate會足夠...

+0

亞歷克斯K的答案像我的手套一樣適合我的問題。每個新表的傳入數據都是未知的類型和大小以及名稱等等。我創建的臨時表的一點額外存儲空間是一個可接受的折衷方案。感謝您的意見。對於被鼓吹的良好習慣有積極的評價。 – callisto

0
DataTier.ClsGlobal dataBaseObj = new DataTier.ClsGlobal(); 

    string strmessage; 
    int intResult; 

    public string login(string username, string password)// login method 
    { 
     dataBaseObj.sqlconn.Open(); 

     dataBaseObj.sqlCmd = new SqlCommand("SELECT clEmail, clPassword FROM Client where clEmail='" + username + "'", dataBaseObj.sqlconn); 
     dataBaseObj.sqlDr = dataBaseObj.sqlCmd.ExecuteReader(); 


     if (dataBaseObj.sqlDr.Read()) 
     { 
      if (dataBaseObj.sqlDr["clPassword"].Equals(password.ToString())) 
      { 
       strmessage = "client"; 
       dataBaseObj.sqlconn.Close(); 
      } 
      else 
      { 
       intResult++; 

       strmessage = "login not succesfull"; 

       dataBaseObj.sqlconn.Close(); 

       if (intResult == 3) 
       { 
        strmessage = "your Blocked"; 
       } 

      } 
     } // if its not the client is gonna go to workers table 
     else 
     { 
      dataBaseObj.sqlCmd = new SqlCommand("SELECT wuUsername, wuPassword, wuUserType FROM WorkUsers where wuUsername'" + username + "'", dataBaseObj.sqlconn); 
      dataBaseObj.sqlDr = dataBaseObj.sqlCmd.ExecuteReader(); 

      if (dataBaseObj.sqlDr.Read()) 
      { 
       if (dataBaseObj.sqlDr["wuPassword"].Equals(password.ToString())) 
       { 
        strmessage = "Receptionist"; 
        dataBaseObj.sqlconn.Close(); 
       } 
       else 
       { 

        intResult++; 

        strmessage = "login not succesful"; 

        dataBaseObj.sqlconn.Close(); 

        if (intResult == 3) 
        { 
         strmessage = "your Blocked"; 
        } 
       } 
      } 
     } 
     return strmessage; 
    } 

    // this method is for registering the client of the client if not registered 
    public string registration(string clID, string clFirstName, string clSurname, string clStreetAddress, string clCity, string clPostCode, string clHomePhone, string clMobilePhone, string clEmail, string clPassword, string clStatus) 
    { 
     dataBaseObj.sqlconn.Open(); 
     dataBaseObj.sqlCmd = new SqlCommand("insert into Client values(@clID,@[email protected],@clStreetAddress,@clCity,@clPostCode,@clHomePhone,@clMobilePhone,@clEmail,@[email protected])", dataBaseObj.sqlconn); 

     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clID", clID); 

     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clFirstName", clFirstName); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clSurname", clSurname); 


     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clStreetAddress", clStreetAddress); 

     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clCity", clCity); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clPostCode", clPostCode); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clHomePhone", clHomePhone); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clMobilePhone", clMobilePhone); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clEmail", clEmail); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clPassword", clPassword); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clStatus", clStatus); 

     intResult = dataBaseObj.sqlCmd.ExecuteNonQuery(); 
     if (intResult == 1) 
     { 
      strmessage = "registeed"; 
     } 
     else 
     { 
      strmessage = "not registeed"; 
     } 

     return strmessage; 
    } 

    // this is for the admin to register the the receptionist 
    public string AdminRegister(string Id, string username, string password) 
    { 
     dataBaseObj.sqlconn.Open(); 
     dataBaseObj.sqlCmd = new SqlCommand("INSERT INTO WorkUsers (wuID, wuUsername, wuPassword) VALUES ('" + Id + "','" + username + "','" + password + "')", dataBaseObj.sqlconn); 
     intResult = intResult = dataBaseObj.sqlCmd.ExecuteNonQuery(); 
     if (intResult == 1) 
     { 
      strmessage = "registeed"; 
     } 
     else 
     { 
      strmessage = "not registeed"; 
     } 

     return strmessage; 
    } 

    // this method is fo receptionist to inser an appoirment 
    public string ReceptioInsert(int appID, string appDateNow, string appTimeNow, string appReason, string appDateofBooking, string appTimeofBooking, int clID, int wuID) 
    { 
     dataBaseObj.sqlconn.Open(); 

     // appDateNow = Convert.ToString(System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.DayOfWeek); 
     appDateNow = Convert.ToString(System.DateTime.Now.ToString("m")); 
     //appTimeNow = Convert.ToString(System.DateTime.Now.TimeOfDay); 
     appTimeNow = Convert.ToString(System.DateTime.Now.ToString("hh:mm:ss")); 

     dataBaseObj.sqlCmd = new SqlCommand("SELECT appDateNow, appTimeNow FROM Appointments where appDateNow='" + appTimeNow + "'AND where appTimeNow='" + appTimeNow + "'", dataBaseObj.sqlconn); 
     if (dataBaseObj.sqlDr.Read())// if it reads it means date and time are booked 
     { 
      strmessage = "sorry that time is not available"; 
     } 
     else 
     { 

      dataBaseObj.sqlCmd = new SqlCommand("INSERT INTO Appointments (appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID) VALUES('" + appID + "','" + appDateNow + "','" + appTimeNow + "','" + appReason + "','" + appDateofBooking + "','" + appTimeofBooking + "','" + clID + "','" + wuID + "')", dataBaseObj.sqlconn); 
      intResult = intResult = dataBaseObj.sqlCmd.ExecuteNonQuery(); 
      if (intResult == 1) 
      { 
       strmessage = "registeed"; 
      } 
      else 
      { 
       strmessage = "not registeed"; 
      } 
      dataBaseObj.sqlconn.Close(); 
     } 
     return strmessage; 

    } 
    // this method is to update the appointment 
    public void updateAppointmen(int appID, string appDateNow, string appTimeNow, string appReason, string appDateofBooking, string appTimeofBooking, int clID, int wuID) 
    { 
     dataBaseObj.sqlconn.Open(); 
     appDateNow = Convert.ToString(System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.DayOfWeek); 

     appTimeNow = Convert.ToString(System.DateTime.Now.TimeOfDay); 
     dataBaseObj.sqlCmd = new SqlCommand("UPDATE Appointments SET appDateNow [email protected], appTimeNow [email protected], appReason [email protected], appDateofBooking [email protected], appTimeofBooking [email protected], clID [email protected], wuID [email protected] where appID='" + appID + "'", dataBaseObj.sqlconn); 



     dataBaseObj.sqlCmd.Parameters.AddWithValue("@appDateNow", appDateNow); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@appTimeNow", appTimeNow); 

     dataBaseObj.sqlCmd.Parameters.AddWithValue("@appReason", appReason); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@appDateofBooking", appDateofBooking); 

     dataBaseObj.sqlCmd.Parameters.AddWithValue("@appTimeofBooking", appTimeofBooking); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clID", clID); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@wuID", wuID); 
     dataBaseObj.sqlCmd.ExecuteNonQuery(); 
    } 
    // this method is to delete the appoinment 

    public void deleteAppointment(int appID) 
    { 
     dataBaseObj.sqlconn.Open(); 
     dataBaseObj.sqlCmd = new SqlCommand("DELETE FROM Appointments whre appID='" + appID + "'"); 
     dataBaseObj.sqlCmd.ExecuteNonQuery(); 
    } 
    // method for client to view the appoinments 
    public DataTable CLientViewAppoitnmet(string date) 
    { 
     dataBaseObj.sqlconn.Open(); 
     dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID FROM Appointments where appDateNow='" + date + "'", dataBaseObj.sqlconn); 
     dataBaseObj.dt = new DataTable("appoitment"); 
     dataBaseObj.sqlAdt.Fill(dataBaseObj.dt); 
     dataBaseObj.sqlconn.Close(); 
     return dataBaseObj.dt; 
    } 

// this method is for the client to insert a note 
    public string CLinetInformRecep(int anID, int appID, string anNotes) 
    { 
     dataBaseObj.sqlconn.Open(); 
     dataBaseObj.sqlCmd = new SqlCommand("INSERT INTO AppointmentNotes (anID, appID, anNotes) VALUES ('" + anID + "','" + appID + "','" + anNotes + "')", dataBaseObj.sqlconn); 
     intResult = intResult = dataBaseObj.sqlCmd.ExecuteNonQuery(); 
      if (intResult == 1) 
      { 
       strmessage = "registeed"; 
      } 
      else 
      { 
       strmessage = "not registeed"; 
      } 
      dataBaseObj.sqlconn.Close(); 
      return strmessage; 
    } 
     // this is for the receptionis to view notes added vy the client 

    public DataTable selectAppointmentNotes() 
    { 


     dataBaseObj.sqlconn.Open(); 
     dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT FROM AppointmentNotes", dataBaseObj.sqlconn); 
     dataBaseObj.dt = new DataTable("appoitment"); 
     dataBaseObj.sqlAdt.Fill(dataBaseObj.dt); 
     dataBaseObj.sqlconn.Close(); 
     return dataBaseObj.dt; 
    } 
    // this methiod is for therapist to view cuurent day appointment 
    public DataTable TherapisViewAppoiments()  
    { 
     dataBaseObj.sqlconn.Open(); 
     dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID FROM Appointments where appDateNow ='" +System.DateTime.Now.DayOfWeek+"'", dataBaseObj.sqlconn); 
     dataBaseObj.dt = new DataTable("appoitment"); 
     dataBaseObj.sqlAdt.Fill(dataBaseObj.dt); 
     dataBaseObj.sqlconn.Close(); 
     return dataBaseObj.dt; 
    } 
    // this method here is for the therapist to view cuurent day appointment with oppoetunit to filter them 
    public DataTable ViewAllAppoiements(int id, string date) 
    { 
     // this method return the information of appointmenet where the id and date matchs since i have user the operatoe AND 
     dataBaseObj.sqlconn.Open(); 
     dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID FROM Appointments where appID ='" + id + "'AND where appDateNow='" + date +"'", dataBaseObj.sqlconn); 
     dataBaseObj.dt = new DataTable("appoitment"); 
     dataBaseObj.sqlAdt.Fill(dataBaseObj.dt); 
     dataBaseObj.sqlconn.Close(); 
     return dataBaseObj.dt; 
    } 
    // this method is to update appoitment by therapist 
    public void UpdateAppoiment(int appID, string appDateNow, string appTimeNow, string appReason, string appDateofBooking, string appTimeofBooking, int clID, int wuID) 
    { 
     dataBaseObj.sqlconn.Open(); 
     appDateNow = Convert.ToString(System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.DayOfWeek); 

     appTimeNow = Convert.ToString(System.DateTime.Now.TimeOfDay); 
     dataBaseObj.sqlCmd = new SqlCommand("UPDATE Appointments SET appDateNow [email protected], appTimeNow [email protected], appReason [email protected], appDateofBooking [email protected], appTimeofBooking [email protected], clID [email protected], wuID [email protected] where appID='" + appID + "'", dataBaseObj.sqlconn); 



     dataBaseObj.sqlCmd.Parameters.AddWithValue("@appDateNow", appDateNow); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@appTimeNow", appTimeNow); 

     dataBaseObj.sqlCmd.Parameters.AddWithValue("@appReason", appReason); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@appDateofBooking", appDateofBooking); 

     dataBaseObj.sqlCmd.Parameters.AddWithValue("@appTimeofBooking", appTimeofBooking); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@clID", clID); 
     dataBaseObj.sqlCmd.Parameters.AddWithValue("@wuID", wuID); 
     dataBaseObj.sqlCmd.ExecuteNonQuery(); 
    } 
    // this method is to delete the appoinmet 
    public string deleteAppoinment(int appID) 
    { 
     dataBaseObj.sqlconn.Open(); 
     dataBaseObj.sqlCmd = new SqlCommand("DELETE FROM Appointments whre appID='" + appID + "'"); 
     dataBaseObj.sqlCmd.ExecuteNonQuery(); 
     intResult = intResult = dataBaseObj.sqlCmd.ExecuteNonQuery(); 
     if (intResult == 1) 
     { 
      strmessage = "registeed"; 
     } 
     else 
     { 
      strmessage = "not registeed"; 
     } 
     dataBaseObj.sqlconn.Close(); 
     return strmessage; 
    } 
    // report order by date 

    public DataTable listOfCLient() 
    { 
     dataBaseObj.sqlconn.Open(); 
     dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT clID, clFirstName, clSurname, clStreetAddress, clCity, clPostCode, clHomePhone, clMobilePhone, clEmail, clPassword, clStatus FROM Client", dataBaseObj.sqlconn); 
     dataBaseObj.dt = new DataTable("appoitment"); 
     dataBaseObj.sqlAdt.Fill(dataBaseObj.dt); 
     dataBaseObj.sqlconn.Close(); 
     return dataBaseObj.dt; 
    } 
    public DataTable listOfAppoint() 
    { 
     dataBaseObj.sqlconn.Open(); 
     dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT  appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID FROM Appointments ORDER BY appDateNow", dataBaseObj.sqlconn); 
     dataBaseObj.dt = new DataTable("appoitment"); 
     dataBaseObj.sqlAdt.Fill(dataBaseObj.dt); 
     dataBaseObj.sqlconn.Close(); 
     return dataBaseObj.dt; 
    } 

    public DataTable listAppontBytherapy() 
    { 
     dataBaseObj.sqlconn.Open(); 
     dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT Appointments.appID, Appointments.appDateNow, Appointments.appTimeNow, Appointments.appReason, Appointments.appDateofBooking,Appointments.appTimeofBooking, Appointments.clID, Appointments.wuID, WorkUsers.wuUserType FROM Appointments INNER JOIN WorkUsers ON Appointments.wuID = WorkUsers.wuID ORDER BY WorkUsers.wuUserType", dataBaseObj.sqlconn); 
     dataBaseObj.dt = new DataTable("appoitment"); 
     dataBaseObj.sqlAdt.Fill(dataBaseObj.dt); 
     dataBaseObj.sqlconn.Close(); 
     return dataBaseObj.dt; 
    } 
} 
} 
相關問題