2016-04-27 18 views
0

Ich versuche, ein Objekt zu erhalten, dessen Startdatum + Startzeit abgelaufen ist oder gleich dem aktuellen Datum ist und dessen Enddatum + Endzeit das aktuelle Datum nicht überschritten hat. Ich habe versucht, das aktuelle Datum in nvarchar umzuwandeln, da alle meine Spalten nvarchar sind. Ich habe auch versucht, meine Spalten in datetime Typ zu konvertieren, aber es funktioniert auch nicht. Bitte helfenSQL Server Datum Zeit

Säule:

startDate - nvarchar (dd/mm/yyyy) 
startTime - nvarchar (hh:mm) 24 hr 
endDate - nvarchar(dd/mm/yyyy) 
endTime - nvarchar(hh:mm) 24hr 

ich habe versucht, aber Datetime betwween startdate + Startzeit und endDate + Startzeit, aber es scheint nicht zu funktionieren:

SELECT * FROM Promo 
WHERE membership = '1' 
AND promoStatus = '1' 
AND CONVERT(NVARCHAR, GetDate(), 101) + ' ' + 
      CONVERT(NVARCHAR, DATEPART(hh, GetDate())) + ':' + 
      RIGHT('0' + CONVERT(NVARCHAR, DATEPART(mi, GetDate())), 2) BETWEEN 
      startDate + ' ' + startTime 
AND endDate + ' ' + endTime 

ich habe versucht andere Methode auch:

SELECT * FROM Promo 
WHERE membership = '1' 
AND promoStatus = '1' 
AND startDate + ' ' + startTime <= CONVERT(NVARCHAR, GetDate(), 101) + ' ' + 
      CONVERT(NVARCHAR, DATEPART(hh, GetDate())) + ':' + 
      RIGHT('0' + CONVERT(NVARCHAR, DATEPART(mi, GetDate())), 2) 
AND endDate + ' ' + endTime >= CONVERT(NVARCHAR, GetDate(), 101) + ' ' + 
      CONVERT(NVARCHAR, DATEPART(hh, GetDate())) + ':' + 
      RIGHT('0' + CONVERT(NVARCHAR, DATEPART(mi, GetDate())), 2) 
+0

Bitte geben Sie Beispieldaten und erwartete Ergebnis. –

Antwort

0

Sie könnten es so tun:

SELECT * 
FROM Promo 
WHERE 
    membership = '1' 
    AND promoStatus = '1' 
    AND CAST(CONVERT(VARCHAR(16), GETDATE(), 120) AS DATETIME) BETWEEN 
     CONVERT(DATETIME, @startDate, 103) + CONVERT(DATETIME, @startTime) 
     AND CONVERT(DATETIME, @endDate, 103) + CONVERT(DATETIME, @endTime) 

Diese Zeile:

CAST(CONVERT(VARCHAR(16), GETDATE(), 120) AS DATETIME) 

das aktuelle Datum Zeit bekommt, um seine Minuten, ist dies die Sekunden und Millisekunden Teil des GETDATE() wird abzustreifen bedeutet.

und diese Zeile:

CONVERT(DATETIME, @startDate, 103) + CONVERT(DATETIME, @startTime) 

Ihre Datums- und Zeitvariablen kombinieren, um einen neuen DATETIME Wert zu bilden.

Beachten Sie, dass Sie damit aufhören könnten, wenn Sie die Tabellen mit den richtigen Datentypen entwerfen.

+0

Danke! es funktioniert perfekt! – lel

0

Versuchen Sie, diese

SELECT * FROM Promo 
WHERE membership = '1' 
AND promoStatus = '1' 
AND CAST(CONVERT(VARCHAR(16), GetDate(), 120) AS DATETIME) 
    BETWEEN CONVERT(DATETIME, startDate + ' ' + startTime, 120) 
     AND CONVERT(DATETIME, endDate + ' ' + endTime, 120)