Príklady:
-- konverzia reálneho císla na celocíselné a rôzne kombinácie s funkciou CAST SELECT 8.5 AS Original, CAST(8.5 AS int) AS int, CAST(8.5 AS decimal(6,4)) AS decimal; -- konverzia reálneho císla na celocíselné a rôzne kombinácie s funkciou CONVERT SELECT 8.5 AS Original, CONVERT(int, 8.5) AS int, CONVERT(real, 8.5) AS real, CONVERT(decimal(6,4), 8.5) AS decimal ; -- konvertovanie cisla na retazec + spajánie retazcov ... SELECT 'Cena tovaru je: ' + CAST(100 AS varchar(12)) AS Price; -- zaokrúhlovanie pomocou pretypovania ... SELECT CAST(11.6496 AS int) as trunc1, CAST(-11.6496 AS int) as trunc2, CAST(11.6496 AS numeric) as round1, CAST(-11.6496 AS numeric) as round2; -- konvertovanie dátumu a casu na retazec v rôznych tvaroch ... SELECT GETDATE() AS "Unconverted Date a time", CAST(GETDATE() AS nvarchar(30)) AS "Using of cast", CONVERT(nvarchar(30), GETDATE(), 126) AS "Using of convert to ISO8601" ; -- konverzie dátumov a casov SELECT '2006-04-25T15:50:59.997' AS UnconvertedText, CAST('2006-04-25T15:50:59.997' AS datetime) AS UsingCast, CONVERT(datetime, '2006-04-25T15:50:59.997', 126) AS UsingConvertFrom_ISO8601 ; -- konverzie retazcov v binárnom tvare na znaky a naopak SELECT CONVERT(char(8), 0x4E616d65, 0) AS "Binary to character Style 0", CONVERT(char(8), 0x4E616d65, 1) AS "Binary to character Style 1", CONVERT(char(8), 0x4E616d65, 2) AS "Binary to character Style 2", CONVERT(binary(8), 'Name', 0) AS "Character to binary Style 0", CONVERT(binary(4), '0x4E616D65', 1) AS "Character to binary Style 1", CONVERT(binary(4), '4E616D65', 2) AS "Character to binary Style 2" ; -- konverzia celého císla na hexadecimálne a naopak SELECT CONVERT(VARBINARY(8), 2100457), CONVERT(INT, 0x00200CE9) ;
Dúfam, že tieto zdrojové SQL kódy pomôžu mnohým programátorom a ušetria veľa času pri vymýšlaní a programovaní.
S pozdravom. Alojz Benďák.