SQL-将数据类型varchar DD / MM / YYYY转换为sql中的日期格式

时间:2018-09-28 10:03:12

标签: sql excel vba azure date

我正在使用Azure SQL数据库。

我正在尝试将包含dd / mm / yyyy格式日期的字段数据类型从varchar(100)转换为日期字段。我没有运气!

任何帮助将不胜感激。我不是SQL专家,但使用Microsoft SQL Server管理。

我的列名是Eventdate,表名是客户。

我已经尝试过,但是我确定自己做错了什么

Select Eventdate
Where Customers
SELECT convert(varchar(100),Eventdate,101)

编辑:

虽然将varchar列更改为datetime的答案很有效,但我现在遇到了另一个问题。我将更详细地介绍数据库的功能。

我有一个具有以下vba代码的excel电子表格,用于连接到sql数据库并将数据插入到SQL。

首先将其发送到名为“ MergeCustomers”的SQL表,检查重复的KeyID字段(JobCode),然后将其放入“ Customer”表中。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim conn As New ADODB.Connection
    Dim iRowNo As Integer
    Dim sCustomerId, sFirstName, sLastName As String


    With Sheets("sql")

        'Open a connection to SQL Server
        conn.Open "Provider=SQLOLEDB;Data Source=tcp:SERVERADDRESS,1433;Initial Catalog=Sales;Persist Security Info=False;User ID=USER;Password=PASSWORD;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"


        'Skip the header row
        iRowNo = 2

        'Loop until empty cell in CustomerId
        Do Until .Cells(iRowNo, 1) = ""
            sJobcode = .Cells(iRowNo, 1)
            sEventstatus = .Cells(iRowNo, 2)
            sVenue = .Cells(iRowNo, 3)
            sGuestno = .Cells(iRowNo, 4)
            sEventtype = .Cells(iRowNo, 5)
            sClient = .Cells(iRowNo, 6)
            sEventdate = .Cells(iRowNo, 7)
            sCommission = .Cells(iRowNo, 8)
            sEventvalue = .Cells(iRowNo, 9)
            sEventmargin = .Cells(iRowNo, 10)
            sAccountman = .Cells(iRowNo, 11)

            'Generate and execute sql statement to import the excel rows to SQL Server table
            conn.Execute "insert into dbo.CustomersStage (Jobcode, Eventstatus, Venue, Guestno, Eventtype, Client, Eventdate, Commission, Eventvalue, Eventmargin, Accountman) values ('" & sJobcode & "', '" & sEventstatus & "', '" & sVenue & "', '" & sGuestno & "', '" & sEventtype & "', '" & sClient & "', '" & sEventdate & "', '" & sCommission & "', '" & sEventvalue & "', '" & sEventmargin & "', '" & sAccountman & "')"

            iRowNo = iRowNo + 1
        Loop

        conn.Execute "EXEC dbo.MergeCustomers"

        MsgBox "File saved and data successfully uploaded to the cloud"

        conn.Close
        Set conn = Nothing

    End With

End Sub

更改了MergeCustomers和客户sql表中的'Eventdate'列后,在excel中运行VBA代码时出现以下错误:

“将varchar数据类型转换为日期时间数据类型导致值超出范围”

我认为我需要更改excel文件的输出以匹配sql数据类型的更改?

我已经复制了在以下SQL上运行的合并过程。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[MergeCustomers] AS

SET NOCOUNT ON

MERGE dbo.Customers AS Trgt
USING dbo.CustomersStage Src
    ON Trgt.Jobcode  = Src.Jobcode
WHEN NOT MATCHED BY TARGET THEN
    INSERT (Jobcode, Eventstatus, Venue, Guestno, Eventtype, Client, Eventdate, Commission, Eventvalue, Eventmargin, Accountman)
    VALUES (Src.Jobcode, Src.Eventstatus, Src.Venue, Src.Guestno, Src.Eventtype, Src.Client, Src.Eventdate, Src.Commission, Src.Eventvalue, Src.Eventvalue, Src.Accountman)
WHEN MATCHED AND
    (
        ISNULL(Trgt.Eventstatus, '') <> ISNULL(Src.Eventstatus, '')
    OR
        ISNULL(Trgt.Venue, '') <> ISNULL(Src.Venue, '')
    OR
        ISNULL(Trgt.Guestno, '') <> ISNULL(Src.Guestno, '')
    OR
        ISNULL(Trgt.Eventtype, '') <> ISNULL(Src.Eventtype, '')
    OR
        ISNULL(Trgt.Client, '') <> ISNULL(Src.Client, '')
    OR
        ISNULL(Trgt.Eventdate, '') <> ISNULL(Src.Eventdate, '')
    OR
        ISNULL(Trgt.Commission, '') <> ISNULL(Src.Commission, '')
    OR
        ISNULL(Trgt.Eventvalue, '') <> ISNULL(Src.Eventvalue, '')
    OR
        ISNULL(Trgt.Eventmargin, '') <> ISNULL(Src.Eventmargin, '')
    OR
        ISNULL(Trgt.Accountman, '') <> ISNULL(Src.Accountman, '')
    )
THEN
    UPDATE SET Eventstatus = Src.Eventstatus, Venue = Src.Venue, Guestno = Src.Guestno, Eventtype = Src.Eventtype, Client = Src.Client, Eventdate = Src.Eventdate, Commission = Src.Commission, Eventvalue = Src.Eventvalue, Eventmargin = Src.Eventmargin, Accountman = Src.Accountman
    ;

DELETE FROM dbo.CustomersStage

2 个答案:

答案 0 :(得分:0)

只是确认。

您是否要仅通过SQL语句将日期从VARCHAR转换为DATE来查看日期?还是完全修改的数据类型?

编辑:

能否提供有关表以及行数据中的一两行的更多信息。

可能的解决方案:

我将在黑暗中刺伤,而无需进一步的细节。

尝试一下:

SET DATEFORMAT dmy
ALTER TABLE Customers
ALTER COLUMN Eventdate DATETIME

答案 1 :(得分:0)

使用sql中的convert函数解决您的问题。下面是代码。

--Create a sample table with EventDate column of varchar type 
Create Table Customers 
(
EventDate varchar(100)
)

Insert into Customers Values (GETDATE())

-- Declare a variable of varchar type to read customer table 
Declare @EventDate varchar(100)
Select @EventDate = EventDate from Customers


--Declare a new variable of the required data type of date  
Declare @EventDateInDateDataType Date 


-- use convert function to convert varchar to date format 
Select @EventDateInDateDataType = CONVERT(Date,@EventDate,101) 
Select @EventDateInDateDataType