>Problema:
>
>Ho un tabella importata da files ASCII (conversioni da vecchio
>gestionale) dove la data
>e' espressa in formato VARCHAR di 8 ddmmyyyy
>
>Devo convertirla in formato data di SQL2000 (lingua inglese)
>
>Quale comando devo utilizzare
Il modo migliore per dare in pasto a SQL Server una stringa contenente una data senza dare adito a fraintendimenti è quello di utilizzare il formato ANSI SQL 'YYYYMMDD'.
Visto che nel tuo caso la data è espressa esttamente al contrario, te la puoi cavare facilmente ricorrendo alla funzione SUBSTRING(), recuperando singolarmente anno, mese e giorno e concatenandoli tra di loro:
USE tempdb;
CREATE TABLE dbo.Source(
SourceID int NOT NULL PRIMARY KEY,
DateString varchar(8) NOT NULL
);
INSERT dbo.Source VALUES(1, '12061971');
INSERT dbo.Source VALUES(2, '31122007');
INSERT dbo.Source VALUES(3, '19022008');
CREATE TABLE dbo.Destination(
DestinationID int NOT NULL PRIMARY KEY,
DateValue datetime NOT NULL
);
INSERT dbo.Destination
SELECT
SourceID
, SUBSTRING(DateString, 5, 4) + -- Anno
SUBSTRING(DateString, 3, 2) + -- Mese
SUBSTRING(DateString, 1, 2) -- Giorno
FROM dbo.Source;
SELECT *
FROM dbo.Destination;
/* Output:
DestinationID DateValue
------------- -----------------------
1 1971-06-12 00:00:00.000
2 2007-12-31 00:00:00.000
3 2008-02-19 00:00:00.000
(3 row(s) affected)
*/
DROP TABLE dbo.Source, dbo.Destination;
SQL Server provvederà a castare implicitamente la stringa in un valore datetime.
>Grazie
Prego.
Ciao!
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
http://italy.mvps.org