我需要在yyyymmdd
格式中添加幾天到varchar
表示形式。將字符串轉換爲yyyymmdd格式的日期時間
在SQL Server中varchar(8)
值轉換爲datetime
時,我得到一個錯誤2005
我沒有線索,爲什麼!
這裏是什麼在起作用:
select
convert (datetime, '20111019')
from _table
這是不行的,雖然在列中的值是20111019
select
convert (datetime, RechDatum)
from _table
我已經嘗試過:
convert (datetime, RechDatum, 112)
和
SET DATEFORMAT ymd
我能用自己的功能解決問題。但我仍然感到困惑,很高興爲您解答!
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'getDateFromString' and type = 'FN')
DROP FUNCTION getDateFromString
GO
CREATE FUNCTION getDateFromString (@Cyear VARCHAR(4), @Cmonth smallint, @Cday smallint, @offsetdays smallint)
RETURNS DATETIME
AS BEGIN
DECLARE @day SMALLINT
DECLARE @month SMALLINT
DECLARE @year SMALLINT
SET @day = CAST (@Cday AS SMALLINT)
SET @month = CAST (@Cmonth AS SMALLINT)
SET @year = CAST (@Cyear AS SMALLINT)
RETURN DateAdd(day, @offsetdays,
(SELECT
DateAdd (day, @day -1,
DateAdd (month, @month -1,
DateAdd (Year, @year-1900,0))) ))
END
GO
下面是表的定義,它是一個德國的ERP系統的一部分:
CREATE TABLE [dbo].[AuftrKopf](
[ID] [int] IDENTITY(1,1) NOT NULL,
[IsSammelRechnung] [bit] NULL,
[AuftragsNr] [int] NULL,
[Kd_ID] [int] NULL,
[Fahrer_ID] [int] NULL,
[Debitor] [int] NULL,
[Fz_ID] [int] NULL,
[FHFz_ID] [int] NULL,
[AuftrArt_ID] [int] NULL,
[AuftrHerkunft_ID] [int] NULL,
[AuftrZustand_ID] [int] NULL,
[RechnungsArt_ID] [int] NULL,
[AuftragVom] [varchar](8) NULL,
[FertigAmUm] [varchar](12) NULL,
[BringenAmUm] [varchar](12) NULL,
[AbgeschlossenAmUm] [varchar](12) NULL,
[StempelkartenNr] [int] NULL,
[HuetchenNr] [int] NULL,
[LeistungsDatum] [varchar](8) NULL,
[RechnDatum] [varchar](8) NULL,
[ValutaDatum] [varchar](8) NULL,
[NettoSummenspalte1] [decimal](18, 2) NULL,
[NettoSummenspalte2] [decimal](18, 2) NULL,
[NettoSummenspalte3] [decimal](18, 2) NULL,
[NettoSummenspalte4] [decimal](18, 2) NULL,
[NettoSummenspalte5] [decimal](18, 2) NULL,
[NettoSummenspalte6] [decimal](18, 2) NULL,
[NettoSummenspalte7] [decimal](18, 2) NULL,
[NettoSummenspalte8] [decimal](18, 2) NULL,
[NettoSummenspalte9] [decimal](18, 2) NULL,
[NettoSummenspalte10] [decimal](18, 2) NULL,
[NettoOhneSummenSpalte] [decimal](18, 2) NULL,
[MwStSummenspalte1] [decimal](18, 2) NULL,
[MwStSummenspalte2] [decimal](18, 2) NULL,
[MwStSummenspalte3] [decimal](18, 2) NULL,
[MwStBasisspalte1] [decimal](18, 2) NULL,
[MwStBasisspalte2] [decimal](18, 2) NULL,
[MwStBasisspalte3] [decimal](18, 2) NULL,
[BasisspalteOhneMwSt] [decimal](18, 2) NULL,
[KdNr] [int] NULL,
[KdNachname] [nvarchar](50) NULL,
[KdVorname] [nvarchar](50) NULL,
[KdTelefon] [nvarchar](30) NULL,
[KdMobilTelefon] [nvarchar](30) NULL,
[KdEmail] [nvarchar](100) NULL,
[Kennz] [nvarchar](20) NULL,
[BerKennz] [nvarchar](20) NULL,
[KM] [int] NULL,
[Laufleistung] [int] NULL,
[RabattStufe] [int] NULL,
[Adresse_ID] [int] NULL,
[CPrLieferschein] [int] NULL,
[BincMwSt] [bit] NULL,
[LandWKuerzel] [nvarchar](4) NULL,
[Bemerkung] [nvarchar](50) NULL,
[Verkaeufer_ID] [int] NULL,
[Sachbearbeiter_ID] [int] NULL,
[PersIDFaktura] [int] NULL,
[Monteur_ID] [int] NULL,
[Team_ID] [int] NULL,
[CPrRepAngebot] [int] NULL,
[CPrRepAuftr] [int] NULL,
[CPrWerkstattkarte] [int] NULL,
[CPrRechnung] [int] NULL,
[CPrRechnungFormularName] [nvarchar](50) NULL,
[CPrRepRechnKopie] [int] NULL,
[CPrPickerzettel] [int] NULL,
[CPrVorabRechn] [int] NULL,
[CPrAuftrBestaet] [int] NULL,
[BOhneFzDaten] [bit] NULL,
[BOhneNr] [bit] NULL,
[PrOhneEinzelpreis] [bit] NULL,
[FHFzNW] [bit] NULL,
[FHDifferenzbesteuert] [bit] NULL,
[StornoZuAuftrNr] [int] NULL,
[Storno_Datum] [varchar](8) NULL,
[Buchungsdatum] [varchar](14) NULL,
[VakBuchungsModus] [int] NULL,
[DiffNettoKompZuSumme] [decimal](18, 2) NULL,
[Vorgangsart_ID] [int] NULL,
[Beschreibung] [ntext] NULL,
[IsErreichbarTel1perSMS] [bit] NULL,
[IsErreichbarTel1perTel] [bit] NULL,
[IsErreichbarMobilperSMS] [bit] NULL,
[IsErreichbarMobilperTel] [bit] NULL,
[IsErreichbarEmail] [bit] NULL,
[StundenGeschaetzt] [decimal](18, 2) NULL,
[WKZ_Zweitwaehrung] [nvarchar](3) NULL,
[Kurs_Zweitwaehrung] [decimal](18, 7) NULL,
[EndbetragInZweitwaehrung] [bit] NULL,
[Kulanzursprung] [int] NULL,
[IsKulanzgesplittet] [bit] NULL,
[SplitUrsprung] [int] NULL,
[IsGesplittet] [bit] NULL,
[FlottenReparaturkostenId] [int] NULL,
[NovaSatz] [decimal](18, 2) NULL,
[NovaGesamt] [decimal](18, 2) NULL,
[NovaBasis] [decimal](18, 2) NULL,
[NovaBonusMalus] [decimal](18, 2) NULL,
[FilialNr] [int] NULL,
[Angebotverfall] [varchar](8) NULL,
[IvecoSonderverkauf] [bit] NULL,
[LetzteAenderung] [varchar](14) NULL,
[IsAnzahlung] [bit] NULL,
[AnzahlungSumme] [decimal](18, 2) NULL,
[SADESperre] [int] NULL,
[SADEVersendet] [varchar](14) NULL,
[MP2Angebotsnummer] [int] NULL,
[SchadenNummer] [nvarchar](20) NULL,
[ReferenzAudaNet] [nvarchar](40) NULL,
[LetzterBearbeiter] [nvarchar](50) NULL,
[Rechnername] [nvarchar](50) NULL,
[ReferenzAudaNetCaseId] [nvarchar](40) NULL,
[AuftragsanlageOrder] [bit] NULL,
[RabattDrucken] [bit] NULL,
[AuftragStatus1_ID] [int] NULL,
[AuftragStatus2_ID] [int] NULL,
[AuftragStatus3_ID] [int] NULL,
[AuftragStatus4_ID] [int] NULL,
[TeileZurueckBeiGutschrift] [bit] NULL,
[StornoGrund_ID] [int] NULL,
[Anleger_ID] [int] NULL,
[Betriebsstunden] [int] NULL,
[SperreSADE] [bit] NULL,
[FiOnlineContractId] [nvarchar](20) NULL,
[DWNichtMehrFragen] [bit] NULL,
[BestellZaehler] [int] NULL,
[NovaOhneAufschlag] [bit] NULL,
[DefaultLager_ID] [int] NULL,
[RechnungBezahltKennzeichnen] [bit] NULL,
[AnVWBackboneAsNewReported] [bit] NULL,
[VWBackboneRequestID] [nvarchar](20) NULL,
[VWBackboneConversationID] [nvarchar](70) NULL,
[VWBackboneStatusID] [int] NULL,
[VWBackboneSaga2ClaimNr] [nvarchar](2) NULL,
[VWBackboneSaga2GarantieAntragNr] [int] NULL,
[VWBackboneSaga2MaxClaims] [int] NULL,
CONSTRAINT [PK_AuftrKopf] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
奇怪的是,該轉換我的電腦與德國和瑞士的設置上工作,但不是在法語設置的客戶端計算機上。
你可以腳本表「作爲創建」,併發佈列定義爲RechDatum? – Andomar
您收到的錯誤消息是什麼? –