I denne artikel vil vi udforske brugen af de forskellige SQL CONVERT-datoformater i SQL Server.
Datofortolkning varierer mellem forskellige lande. Antag, at du har en global SQL Server-database med en tabel, der indeholder et bestemt datoformat. For eksempel har den en datokolonne, der har værdien 01/05/2020.
Hvordan tolker du det? Lad os se på følgende fortolkninger på tværs af forskellige lande.
- USA:5. januar 2020 (standardformat – mm/dd/åååå)
- Europa:1. maj 2020 (standardformat – dd/mm/åååå)
Derudover følger andre lande forskellige datoformater:
- Tyrkiet:dd.mm.åååå
- Indien:dd-mm-åååå
- Bulgarien:åååå-m-d
- Ungarn:åååå.mm.dd.
Du kan henvise til Wikipedia for at få flere oplysninger om datoformater efter land.
Ud over dette vil vi nogle gange også gerne inkludere tidsstemplet sammen med datoer. Et par eksempler på dette er:
- 01/05/2020 10:00
- 0/05/2020 14:00
- 05/00/2020 14:00
- 01/05/2020 02:00:55 AM
Det er ikke muligt at gemme datoer i en SQL Server-tabel i forskellige formater, så vi har brug for en måde at konvertere datoformater på. Lad os udforske de forskellige SQL CONVERT-datoformateringsmetoder.
SQL KONVERT-datofunktion
Databaseprofessionelle bruger typisk SQL CONVERT-datofunktionen til at få datoer i et specificeret og konsistent format. Dette anvender stilkoderne for specifikke outputdatoer.
Syntaks for CONVERT()-funktionen:
KONVERTER(datatype, datotid [,stil])
I nedenstående SQL-forespørgsel konverterer vi datetime til to formater ved hjælp af CONVERT()-funktionen.
- mm/dd/åå-format:stilkode 1
- mm/dd/åååå-format:stilkode 101
DECLARE @Inputdate datetime = '2019-12-31 14:43:35.863'; Select CONVERT(varchar,@Inputdate,1) as [mm/dd/yy], CONVERT(varchar,@Inputdate,101) as [mm/dd/yyyy]
På samme måde kan vi specificere forskellige stilkoder, så du kan konvertere datoer til dit ønskede format.
SELECT '0' AS [StyleCode], 'Default format' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 0) AS [OutputFormat] UNION ALL SELECT '1' AS [StyleCode], 'USA - mm/dd/yy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 1) AS [OutputFormat] UNION ALL SELECT '2' AS [StyleCode], 'ANSI - dd.mm.yy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 2) AS [OutputFormat] UNION ALL SELECT '3' AS [StyleCode], 'British and French - dd/mm/yy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 3) AS [OutputFormat] UNION ALL SELECT '4' AS [StyleCode], 'German - dd.mm.yy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 4) AS [OutputFormat] UNION ALL SELECT '5' AS [StyleCode], 'Italian - dd-mm-yy ' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 5) AS [OutputFormat] UNION ALL SELECT '6' AS [StyleCode], 'Shortened month name -dd mon yy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 6) AS [OutputFormat] UNION ALL SELECT '7' AS [StyleCode], 'Shortened month name - mon dd, yy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 7) AS [OutputFormat] UNION ALL SELECT '8' AS [StyleCode], '24 hour time -hh:mm:ss' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 8) AS [OutputFormat] UNION ALL SELECT '9' AS [StyleCode], 'Default + milliseconds - mon dd yyyy hh:mm:ss:mmmAM (or PM)' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 9) AS [OutputFormat] UNION ALL SELECT '10' AS [StyleCode], 'USA - mm-dd-yy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 10) AS [OutputFormat] UNION ALL SELECT '11' AS [StyleCode], 'Japan -yy/mm/dd' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 11) AS [OutputFormat] UNION ALL SELECT '12' AS [StyleCode], 'ISO format -yymmdd' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 12) AS [OutputFormat] UNION ALL SELECT '13' AS [StyleCode], 'Europe default + milliseconds -dd mon yyyy hh:mm:ss:mmm' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 13) AS [OutputFormat] UNION ALL SELECT '14' AS [StyleCode], ' 24 hour time with milliseconds -hh:mm:ss:mmm(24h)' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 14) AS [OutputFormat] UNION ALL SELECT '20' AS [StyleCode], 'ODBC canonical -yyyy-mm-dd hh:mm:ss(24h)' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 20) AS [OutputFormat] UNION ALL SELECT '21' AS [StyleCode], 'ODBC canonical (with milliseconds) -yyyy-mm-dd hh:mm:ss.mmm' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 21) AS [OutputFormat] UNION ALL SELECT '22' AS [StyleCode], 'mm/dd/yy hh:mm:ss AM (or PM)' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 22) AS [OutputFormat] UNION ALL SELECT '23' AS [StyleCode], 'ISO 8601 - yyyy-mm-dd' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 23) AS [OutputFormat] UNION ALL SELECT '100' AS [StyleCode], 'mon dd yyyy hh:mmAM' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 100) AS [OutputFormat] UNION ALL SELECT '101' AS [StyleCode], 'USA -mm/dd/yyyy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 101) AS [OutputFormat] UNION ALL SELECT '102' AS [StyleCode], 'ANSI -yyyy.mm.dd' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 102) AS [OutputFormat] UNION ALL SELECT '103' AS [StyleCode], 'British/French -dd/mm/yyyy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 103) AS [OutputFormat] UNION ALL SELECT '104' AS [StyleCode], 'German - dd.mm.yyyy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 104) AS [OutputFormat] UNION ALL SELECT '105' AS [StyleCode], 'Italian -dd-mm-yyyy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 105) AS [OutputFormat] UNION ALL SELECT '106' AS [StyleCode], 'Shortened month name -dd mon yyyy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 106) AS [OutputFormat] UNION ALL SELECT '107' AS [StyleCode], 'Shortened month name -mon dd, yyyy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 107) AS [OutputFormat] UNION ALL SELECT '108' AS [StyleCode], '24 hour time -hh:mm:ss' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 108) AS [OutputFormat] UNION ALL SELECT '109' AS [StyleCode], 'Default + milliseconds -mon dd yyyy hh:mm:ss:mmmAM (or PM) ' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 109) AS [OutputFormat] UNION ALL SELECT '110' AS [StyleCode], 'USA -mm-dd-yyyy' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 110) AS [OutputFormat] UNION ALL SELECT '111' AS [StyleCode], 'JAPAN -yyyy/mm/dd' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 111) AS [OutputFormat] UNION ALL SELECT '112' AS [StyleCode], 'ISO -yyyymmdd' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 112) AS [OutputFormat] UNION ALL SELECT '113' AS [StyleCode], 'Europe default + milliseconds -dd mon yyyy hh:mm:ss:mmm' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 113) AS [OutputFormat] UNION ALL SELECT '114' AS [StyleCode], ' 24 hour time with milliseconds -hh:mm:ss:mmm(24h)' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 114) AS [OutputFormat] UNION ALL SELECT '120' AS [StyleCode], 'ODBC canonical- yyyy-mm-dd hh:mm:ss(24h)' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 120) AS [OutputFormat] UNION ALL SELECT '121' AS [StyleCode], 'ODBC canonical (with milliseconds) -yyyy-mm-dd hh:mm:ss.mmm' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 121) AS [OutputFormat] UNION ALL SELECT '126' AS [StyleCode], 'ISO8601 -yyyy-mm-ddThh:mm:ss.mmm' AS [Standard and Format], CONVERT(VARCHAR(50), Getdate(), 126) AS [OutputFormat] UNION ALL SELECT '127' AS [StyleCode], 'ISO8601 with time zone Z-yyyy-mm-ddThh:mm:ss.mmmZ' AS [Standard and Format], CONVERT(VARCHAR(100), Getdate(), 127) AS [OutputFormat] UNION ALL SELECT '131' AS [StyleCode], 'Arabic Hijri date - Islamic calendar' AS [Standard and Format], CONVERT(VARCHAR(100), Getdate(), 131) AS [OutputFormat]
På nedenstående skærmbillede kan du se stilkoden, deres standarder, formater og outputdatoer.
Konvertering af datoer ved hjælp af FORMAT()-funktionen
I ovenstående CONVERT() funktion er vi forpligtet til at specificere stilkoder for et specifikt formatoutput. Normalt ønsker vi ikke at skulle huske disse koder; derfor introducerede Microsoft funktionen FORMAT() i SQL Server 2012.
Syntaksen er vist nedenfor.
FORMAT (værdi, format [, kultur])
Værdi :Det kræver en værdi i det understøttede format. Du kan se Microsoft-dokumentationen for en detaljeret liste.
Format :I formatet kan vi angive formatkoderne eller mønsteret for at dække inputdatodataene. Nedenstående script viser formatkoder, mønster og outputformat.
DECLARE @InputDate DATETIME = '2020-12-08 15:58:17.643' SELECT 'd' AS [FormatCode], 'Short Date Pattern' AS 'Pattern', Format(@InputDate, 'd') AS 'Output' UNION ALL SELECT 'D' AS [FormatCode], 'Long Date Pattern' AS 'Pattern', Format(@InputDate, 'D') AS 'Output' UNION ALL SELECT 'f' AS [FormatCode], 'Full Date/Time pattern (Short Time)' AS 'Pattern', Format(@InputDate, 'f') AS 'Output' UNION ALL SELECT 'F' AS [FormatCode], 'Full Date/Time pattern (Long Time)' AS 'Pattern', Format(@InputDate, 'F') UNION ALL SELECT 'g' AS [FormatCode], 'General Date/Time pattern (Short Time)' AS 'Pattern', Format(@InputDate, 'g') UNION ALL SELECT 'G' AS [FormatCode], 'General Date/Time pattern (Long Time)' AS 'Pattern', Format(@InputDate, 'G') AS 'Output' UNION ALL SELECT 'm' AS [FormatCode], 'Month/Day pattern' AS 'Pattern', Format(@InputDate, 'm') AS 'Output' UNION ALL SELECT 'O' AS [FormatCode], 'Round trip Date/Time pattern' AS 'Pattern', Format(@InputDate, 'O') AS 'Output' UNION ALL SELECT 'R' AS [FormatCode], 'RFC1123 pattern' AS 'Pattern', Format(@InputDate, 'R') AS 'Output' UNION ALL SELECT 's' AS [FormatCode], 'Sortable Date/Time pattern' AS 'Pattern', Format(@InputDate, 's') AS 'Output' UNION ALL SELECT 't' AS [FormatCode], 'Short Time pattern' AS 'Pattern', Format(@InputDate, 't') AS 'Output' UNION ALL SELECT 'T' AS [FormatCode], 'Long Time Pattern' AS 'Pattern', Format(@InputDate, 'T') AS 'Output' UNION ALL SELECT 'u' AS [FormatCode], 'Universal sortable Date/Time pattern' AS 'Pattern', Format(@InputDate, 'u') AS 'Output' UNION ALL SELECT 'U' AS [FormatCode], 'Universal Full Date/Time pattern' AS 'Pattern', Format(@InputDate, 'U') AS 'Output' UNION ALL SELECT 'Y' AS [FormatCode], 'Year Month pattern' AS 'Pattern', Format(@InputDate, 'Y') AS 'Output'
Kultur :Det er et valgfrit argument og definerer kulturen. Hvis vi ikke angiver nogen kultur, bruger SQL Server sproget for den aktuelle session.
I nedenstående forespørgsel konverterer vi et datoformat til en specificeret kultur. Vi er nødt til at specificere kulturkoden. For eksempel er kulturkoden for USA en-US, og hi-IN er for Indien.
Scriptet bruger d formatkode til korte datomønstre.
DECLARE @d DATETIME ='2020-12-08 16:36:17.760'; SELECT FORMAT (@d, 'd', 'en-US') AS 'US English', FORMAT (@d, 'd', 'no') AS 'Norwegian Result', FORMAT(@d, 'd', 'hi-IN') AS 'India', FORMAT(@d, 'd', 'ru-RU') AS 'Russian', FORMAT(@d, 'd', 'gl-ES') AS 'Galician (Spain)', FORMAT ( @d, 'd', 'en-gb' ) AS 'Great Britain English', FORMAT (@d, 'd', 'zu') AS 'Zulu', FORMAT ( @d, 'd', 'de-de' ) AS 'German', FORMAT ( @d, 'd', 'zh-cn' ) AS 'Simplified Chinese (PRC)';
Du får datoformatet i en specificeret kultur, som vist nedenfor.
Hvis du vil have datooutputtet i fuld dato/tid (lang tid), skal du angive formatkoden F, og det ændrer hurtigt dit datoformat.
I datoformatet kan du også angive de brugerdefinerede formater, og det konverterer inputdatostrengen i henhold til dine krav.
For at specificere de tilpassede strenge kan vi bruge følgende forkortelser.
- dd:Dag i måneden (01 til 31)
- dddd:dagstavning
- MM:Månedsnummer (01 til 12)
- MMMM:Måneds stavemåde
- åå:Årstal med to cifre
- åååå:Firecifret år
- hh:Det er klokken 01 til 12
- HH:Det giver 24 timer. format time 00 til 23
- mm:minut 00 til 59
- ss:sekund fra 00 til 59
- tt:AM eller PM
I nedenstående script konverterede vi inputdatoformaterne til flere formater ved hjælp af ovenstående forkortelser eller koder.
DECLARE @d DATETIME ='2020-12-08 16:36:17.760'; SELECT FORMAT (@d,'dd/MM/yyyy ') as [Date Format 1] , FORMAT (@d, 'dd/MM/yyyy, hh:mm:ss ') as [Date Format 2] , FORMAT(@d,'yyyy-MM-dd HH:mm:ss')as [Date Format 3] , FORMAT(@d,'Dd MMM yyyy HH:mm:ss')as [Date Format 4] , FORMAT(@d,'MMM d yyyy h:mm:ss')as [Date Format 5] , FORMAT (@d, 'dddd, MMMM, yyyy')as [Date Format 6] , FORMAT (@d, 'MMM dd yyyy') as [Date Format 7] , FORMAT (@d, 'MM.dd.yy') as [Date Format 8] , FORMAT (@d, 'MM-dd-yy') as [Date Format 9] , FORMAT (@d, 'hh:mm:ss tt')as [Date Format 10] , FORMAT (@d, 'd-M-yy')as [Date Format 11] , FORMAT(@d,'MMMM dd,yyyy')as [Date Format 12]
Brug af AT TIME ZONE i SQL Server 2016 eller nyere
Forskellige lande følger forskellige tidszoner. Normalt følger disse tidszoner offset fra UTC-tid (Coordinated Universal Time). Et par eksempler på tidszoner er:
- Australsk central dagslystid:UTC +10:30
- Indien standardtid:UTC +5:30
- Mountain Daylight Time:UTC-6
- Singapore-tid:UTC+8
- Central dagslystid:UTC-5
Du kan se denne artikel for en detaljeret liste over tidszoner.
Mange lande følger sommertid, og uret justeres 1 time (eller 30-45 minutter) afhængigt af tidszonerne. For eksempel fulgte Central Daylight Time nedenstående tidsplan:
- Standardtid begyndte:1. november 2020 kl. 02:00 lokal tid. Uret blev skruet en time tilbage.
- Standardtid slutter den 14. marts 2021 kl. 02.00 lokal tid. Urene går en time frem.
SQL Server er ikke opmærksom på disse tidszoner og sommertid. Normalt følger en organisation UTC-zonerne, da den ikke kræver nogen ændringer.
Hvordan kan vi konvertere tidszoner i SQL Server?
Du kan bruge AT TIME ZONE fra SQL Server 2016 og konvertere tidszonerne. I nedenstående forespørgsel viser den datoer for Central Standard Time, India Standard Time og Samoa Standard Time.
Declare @DateinUTC datetime2='2020-11-01 02:00:00' select @DateinUTC AT TIME ZONE 'UTC' AT TIME ZONE 'Central Standard Time' as 'Central Standard Time' , @DateinUTC AT TIME ZONE 'UTC' AT TIME ZONE 'India Standard Time' as 'India Standard Time', @DateinUTC AT TIME ZONE 'UTC' AT TIME ZONE 'Samoa Standard Time' as 'Samoa Standard Time', @DateinUTC AT TIME ZONE 'UTC' AT TIME ZONE 'Dateline Standard Time' as 'Dateline Standard Time'
For at kende de understøttede tidszoner kan du forespørge sys.time_zone_info og det returnerer tidszonen og offset.
Du kan derefter filtrere de tidszoner, der i øjeblikket overholder sommertid.
Select * from sys.time_zone_info where is_currently_dst=1
Lad os nu overveje sommertid for Eastern Time.
- Sommertid begyndte – søndag den 8. marts 2020 kl. 02.00.
- Sommertid sluttede – søndag den 1. november 2020 kl. 02.00.
Konverter din UTC-zone til Eastern Standard Time, og du kan bemærke effekten af sommertid.
DECLARE @PreDST datetime = '2020-03-08 06:59:00', @PostDST datetime = '2020-03-08 07:00:00'; SELECT @PreDST AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time' AS [EST time before DST], @PostDST AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time' AS [EST time before DST];
Nyttige punkter til brug af SQL CONVERT-datoformater
Evaluer dine ansøgningskrav, og vælg den relevante datatype dato, SmallDateTime, DateTime, DateTime2 og DateTimeOffset.
Du kan konvertere datoformatet ved hjælp af SQL CONVERT date og FORMAT funktioner; det er dog tilrådeligt at bruge det format, der bedst opfylder dine arbejdsbyrder. Dette vil hjælpe dig med at undgå at skulle bruge den eksplicitte datokonvertering.
Du kan tage højde for sommertid i SQL Server ved at bruge AT TIME ZONE-funktionen fra SQL