使用SQL Server CONVERT将Date转换为yyyyMMddhhmmss

时间:2014-02-08 12:16:07

标签: asp.net sql-server datetime varchar

我需要通过将SQL Server datetime值(例如:20-Feb-14 12:52:48 PM)转换为yyyyMMddhhmmss

来比较日期

我尝试了以下但仍然需要替换空格和“:”并且不能进行多次替换

REPLACE(RIGHT(CONVERT(VARCHAR(19), (OrderDate), 120), 19), '-', '') 

有什么想法吗?

这是我的完整代码:

    Dim dsOrders As New DataSet
    Dim daOrders As SqlDataAdapter
    Dim Cnn As New SqlClient.SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
    Dim strSQL As String
    strSQL = "Select O.OrderID,O.Status,O.OrderDate, O.DeliveryDate, OD.Title,OD.Data from SPLBL_Orders O join SPLBL_OrderData OD on O.OrderID=OD.OrderID  where OD.LanguageID=1"

    '''' Generate Filter Results

    If txtKeyword.Text.ToString <> "" Then
        strSQL = strSQL + " OR OD.Data Like N'%" + txtKeyword.Text.ToString + "%'"
    End If

    If txtMemberID.Text.ToString <> "" Then
        strSQL = strSQL + " and O.MemberID = " + txtMemberID.Text.ToString
    End If

    If chkFeatured.Checked Then
        strSQL = strSQL + " and O.Featured=1"
    End If

    Dim lstStatus As ListItem
    Dim strStatus As String = ""
    For Each lstStatus In ddlStatus.Items
        If lstStatus.Selected Then
            If strStatus <> "" Then
                strStatus = strStatus + ","
            End If
            strStatus += lstStatus.Value.ToString()
        End If
    Next
    If strStatus <> "" Then
        strSQL = strSQL + " and O.Status IN(" + strStatus + ")"
    End If

    If txtStartDate.Text <> "" Then
        Dim strSdate As DateTime = txtStartDate.Text.ToString
        Dim strStart = strSdate.ToString("yyyyMMddhhmmss")
        If txtEndDate.Text <> "" Then
            Dim strEdate As DateTime = txtEndDate.Text.ToString
            Dim strEnd As String
            If txtEndDate.Text <> "" Then
                strEnd = strEdate.ToString("yyyyMMddhhmmss")
            Else
                strEnd = Date.Today.ToString("yyyyMMddhhmmss")
            End If
            strSQL = strSQL + " and REPLACE(REPLACE(REPLACE((CONVERT(VARCHAR(19), (OrderDate), 120)), ':', ''), '-', ''), ' ', '') Between " + strStart + " and " + strEnd + ""
        End If
    End If

    strSQL = strSQL + " order by OD.OrderID desc"

1 个答案:

答案 0 :(得分:1)

你正在给自己造成不必要的麻烦,因为你正在努力解决这个问题。您应该为此使用参数化SQL(以及任何时候想要将变量值传递给SQL查询):

strSQL = strSQL + " and OrderDate Between @dateStart and @dateEnd"

然后将您的开始日期和结束日期作为DateTime参数传递给查询,其名称为@dateStart@dateEnd

有关在VB中使用参数化查询的信息:
How do I create a parameterized SQL query? Why Should I?

这基本上是你需要做的。请注意,我已经创建了一个SqlCommand对象,并在构建查询时为其添加了参数。执行实际查询时,您需要使用此SqlCommand对象。

Dim dsOrders As New DataSet
Dim daOrders As SqlDataAdapter
Dim Cnn As New SqlClient.SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
Dim Cmd As New SqlCommand()
Cmd.Connection = Cnn

Dim strSQL As String
strSQL = "Select O.OrderID,O.Status,O.OrderDate, O.DeliveryDate, OD.Title,OD.Data from SPLBL_Orders O join SPLBL_OrderData OD on O.OrderID=OD.OrderID  where OD.LanguageID=1"

'''' Generate Filter Results

If txtKeyword.Text <> "" Then
    strSQL += " OR OD.Data Like @keyword"
    Cmd.Parameters.AddWithValue("@keyword", "%" + txtKeyword.Text + "%")
End If

If txtMemberID.Text <> "" Then
    strSQL += " and O.MemberID = @memberId"
    Cmd.Parameters.AddWithValue("@memberId", txtMemberID.Text)
End If

If chkFeatured.Checked Then
    strSQL += " and O.Featured=1"
End If

Dim lstStatus As ListItem
Dim statusCount As Integer = 1
Dim strStatus As String = ""
For Each lstStatus In ddlStatus.Items
    If lstStatus.Selected Then
        Dim paramName As String = "@status" + statusCount
        strStatus += ", " + paramName
        Cmd.Parameters.AddWithValue(paramName, lstStatus.Value.ToString)
        statusCount += 1
    End If
Next
If strStatus <> "" Then
    strSQL += " and O.Status IN(" + strStatus.Substring(2) + ")"
End If

If txtStartDate.Text <> "" Then
    Dim startDate As DateTime = DateTime.Parse(txtStartDate.Text)
    Dim endDate As DateTime
    If txtEndDate.Text <> "" Then
        endDate = DateTime.Parse(txtEndDate.Text)
    Else
        endDate = DateTime.Today
    End If
    strSQL += " and OrderDate Between @startDate and @endDate"
    Cmd.Parameters.AddWithValue("@startDate", startDate)
    Cmd.Parameters.AddWithValue("@endDate", endDate)
End If

strSQL += " order by OD.OrderID desc"