BaşlayınÜcretsiz başlayın

Convert strings to dates

The CONVERT() function behaves similarly to CAST(). When translating strings to dates, the two functions do exactly the same work under the covers. Although we used all three parameters for CONVERT() during a prior exercise in Chapter 1, we will only need two parameters here: the data type and input expression.

In this exercise, we will once again look at a table called dbo.Dates. This time around, we will get dates in from our German office. In order to handle German dates, we will need to use SET LANGUAGE to change the language in our current session to German. This affects date and time formats and system messages.

Try querying the dbo.Dates table first to see how things differ from the prior exercise.

Bu egzersiz, kursun bir parçasıdır

Time Series Analysis in SQL Server

Kursa Göz Atın

Egzersiz talimatları

  • Use the CONVERT() function to translate DateText into a date data type.
  • Then use the CONVERT() function to translate DateText into a DATETIME2(7) data type.

Uygulamalı etkileşimli egzersiz

Bu egzersizi bu örnek kodu tamamlayarak deneyin.

SET LANGUAGE 'GERMAN'

SELECT
	d.DateText AS String,
	-- Convert to DATE
	___(___, d.DateText) AS StringAsDate,
	-- Convert to DATETIME2(7)
	___(___, d.DateText) AS StringAsDateTime2
FROM dbo.Dates d;
Kodu Düzenle ve Çalıştır