Meilleur auteur de réponses
Supprimer des Espaces SSIS "Script VB"

Question
-
J'utilise SSIS pour convertir une date "String" en Type "Date", mais le probleme si j'ai une champ n'es pas renseigné j'ai une erreur car dans la base est présenté avec 6 espaces .
ma question est ce que je peux remplacer des espaces pas un champ vide vers ma base de destination
Code
If Not (Row.dateOuver_IsNull) Then
Dim annee1 As Integer = Integer.Parse(Row.dateOuver.Substring(0, 4))
Dim mois1 As Integer = Integer.Parse(Row.dateOuver.Substring(4, 2))
Dim jour1 As Integer = Integer.Parse(Row.dateOuver.Substring(6, 2))
Row.TdateOuver = New DateTime(annee1, mois1, jour1)
Row.TANNEEC = annee1
Row.TMOISC = mois1
Row.TJOURC = jour1
End Ifexemple
base source ---> base destin
20120102 ---> 2012/01/02 :)
20120225 ---> 2012/02/25 :)
---> Null :(
problème:
à System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
à System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
à SC_58c4ef4fb6c64204bd2f5f209dc18828.vbproj.ScriptMain.Entrée0_ProcessInputRow(Entrée0Buffer Row)
à SC_58c4ef4fb6c64204bd2f5f209dc18828.vbproj.UserComponent.Entrée0_ProcessInput(Entrée0Buffer Buffer)
à SC_58c4ef4fb6c64204bd2f5f209dc18828.vbproj.UserComponent.ProcessInput(Int32 InputID, PipelineBuffer Buffer)
à Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.ProcessInput(Int32 inputID, PipelineBuffer buffer)
Réponses
-
Bonjour
C bon j'ai trouvé la réponse
Dim rg As New Regex("[0-9]{8}")
Dim dateNull As DateTime
dateNull = #12:00:00 AM#
If (rg.IsMatch(Row.dateOuver)) Then
Dim annee1 AsInteger = Integer.Parse(Row.dateOuver.Substring(0, 4))
Dim mois1 AsInteger = Integer.Parse(Row.dateOuver.Substring(4, 2))
Dim jour1 AsInteger = Integer.Parse(Row.dateOuver.Substring(6, 2))
Dim date1 As DateTime = jour1.ToString & "/" & mois1.ToString & "/" & annee1.ToString
Row.TOPCJCLOT = FormatDateTime(date1, DateFormat.ShortDate)
Row.TANNEEC = annee1
Row.TMOISC = mois1
Row.TJOURC = jour1
Else
Row.dateOuver = FormatDateTime(dateNull, DateFormat.ShortDate)
- Marqué comme réponse Florin CiucaModerator mardi 31 juillet 2012 18:09
Toutes les réponses
-
Bonjour
C bon j'ai trouvé la réponse
Dim rg As New Regex("[0-9]{8}")
Dim dateNull As DateTime
dateNull = #12:00:00 AM#
If (rg.IsMatch(Row.dateOuver)) Then
Dim annee1 AsInteger = Integer.Parse(Row.dateOuver.Substring(0, 4))
Dim mois1 AsInteger = Integer.Parse(Row.dateOuver.Substring(4, 2))
Dim jour1 AsInteger = Integer.Parse(Row.dateOuver.Substring(6, 2))
Dim date1 As DateTime = jour1.ToString & "/" & mois1.ToString & "/" & annee1.ToString
Row.TOPCJCLOT = FormatDateTime(date1, DateFormat.ShortDate)
Row.TANNEEC = annee1
Row.TMOISC = mois1
Row.TJOURC = jour1
Else
Row.dateOuver = FormatDateTime(dateNull, DateFormat.ShortDate)
- Marqué comme réponse Florin CiucaModerator mardi 31 juillet 2012 18:09
-
De façon plus général, essayez d'utiliser plutôt la méthode TryParse de Integer que Parse. C'est plus rapide et "plus safe".
Richard Clark
Consultant - Formateur .NET
http://www.c2i.fr
Depuis 1996: le 1er site .NET francophone -
Bonjour,
Pardon mais je ne suis pas sur de comprendre vu le titre ambiguë (Quand vous parlez de Script VB)
Il me semble pour tester la date dans une Row qu'il doit y avoir une propriété qui doit retourner si la val est null ou pas (bool)
Puis pour récupérer la date au format essayer :Dim DateActuelle As String = "20120102" Dim t = DateTime.ParseExact(DateActuelle, "yyyyMMdd", CultureInfo.InvariantCulture) Console.WriteLine("L'année : {0}, Le mois : {1}, Le jour : {2}", t.Year, t.Month, t.Day)
C'est plus simple
Cordialement,
- Proposé comme réponse TroxsaEditor mercredi 4 juillet 2012 20:10
-
Et pourquoi pas TryParseExact ? (je sais, j'insiste)
Richard Clark
Consultant - Formateur .NET
http://www.c2i.fr
Depuis 1996: le 1er site .NET francophone -
Et pourquoi pas TryParseExact ? (je sais, j'insiste)
Richard Clark
Consultant - Formateur .NET
http://www.c2i.fr
Depuis 1996: le 1er site .NET francophoneSur les testes que j'ai fait, oui, vous avez raison sur "le plus safe" mais cela n'est pas plus rapide. A moins que je me trompe dans l'exemple ci dessous ?
L'année : 2012, Le mois : 1, Le jour : 2 00:00:00.0003795 L'année : 2012, Le mois : 1, Le jour : 2 00:00:00.0198817
Dim DateActuelle As String = "20120102" Dim stopWatch As New Stopwatch() stopWatch.Start() Dim t = DateTime.ParseExact(DateActuelle, "yyyyMMdd", CultureInfo.InvariantCulture) Console.WriteLine("L'année : {0}, Le mois : {1}, Le jour : {2}", t.Year, t.Month, t.Day) stopWatch.Stop() Console.WriteLine(stopWatch.Elapsed) Dim stopWatch2 As New Stopwatch() stopWatch2.Start() Dim dateVal As DateTime Dim t2 = DateTime.TryParseExact(DateActuelle, "yyyyMMdd", New CultureInfo("fr-FR"), DateTimeStyles.None, dateVal) Console.WriteLine("L'année : {0}, Le mois : {1}, Le jour : {2}", dateVal.Year, dateVal.Month, dateVal.Day) stopWatch2.Stop() Console.WriteLine(stopWatch2.Elapsed)
Cordialement,