sql 转换数据,CAST,CONVERT,TRY_CAST,TRY_CONVERT,FORMAT,PARSE.sql
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 转换数据,CAST,CONVERT,TRY_CAST,TRY_CONVERT,FORMAT,PARSE.sql相关的知识,希望对你有一定的参考价值。
/**************************
CAST AND TRY_CAST
**************************/
/*
CAST - standard way to explicitly converts one data type to another so that you can use one data
type in ways reserved for other data types (for instance, if you want to concatinate an integer
with a text string, you can CAST the integer as a string so that it can be concatenated with
the other string). If the conversion of a row fails, the entire statement will fail.
*/
SELECT CAST(ProductID AS varchar(5)) AS 'ProductID as a varchar data type'
FROM customers1;
/*
TRY_CAST - explicitly converts one data type to another so that you can use one data type in ways
reserved for other data types (for instance, if you want to concatinate an integer with a text
string, you can TRY_CAST the integer as a string so that it can be concatenated with the other
string). If you try with a TRY_CAST, it'll cast what it can and return NULLs for those values that
can't be converted (if you use CAST and one of the values being CAST won't convert, the entire
statement will fail and you'll get an error).
*/
SELECT TRY_CAST(ProductID AS varchar(5)) AS 'ProductID as a varchar data type'
FROM customers1;
/**************************
CONVERT AND TRY_CONVERT
**************************/
/*
CONVERT - similar to CAST, but is useful for dates as it has extra options for formatting dates.
Non-standard, only recognized by SQL Server.
*/
SELECT CONVERT(varchar(5), ProductID) AS 'ProductID as a varchar data type'
FROM customers1;
-- Example of CONVERT that uses codes that convert dates (the '126' in the statement below - see the paragraph note at the bottom of this page to see some of the common date format codes you can use). The example SELECTs a date column as-is, SELECTs and CONVERTs as nvarchar, and then SELECTs and CONVERTs as nvarchar in the ISO8601 format that is one of the common date standards
SELECT SellStartDate,
CONVERT(nvarchar(30), SellStartDate) AS ConvertedDate,
CONVERT(nvarchar(30), SellStartDate, 126) AS ISO8601FormatDate
FROM customers1
-- CONVERT using VARCHAR type and code 1 will place comma separators into a number (for example, the number 23534.12000 will be returned as 23,534.00)
SELECT *, CONVERT(varchar(20), Amount, 1) AS 'Converted Amount' FROM windowTester
ORDER BY Territory, SalesPerson
-- TRY_CONVERT
SELECT TRY_CONVERT(varchar(5), ProductID) AS 'ProductID as a varchar data type'
FROM customers1;
/*
Some of the common CONVERT codes you can use to get certain types of dates returned.
CODE, DATA FORMAT RETURNED, (STANDARD)
100, mon dd yyyy hh:miAM/PM (Default)
101, mm/dd/yyyy (US)
102, yyyy.mm.dd (ANSI)
103, dd/mm/yyyy (British/French)
104, dd.mm.yyyy (German)
105, dd-mm-yyyy (Italian)
106, dd mon yyyy
107, Mon dd, yyyy
108, hh:mm:ss
109, mon dd yyyy hh:mi:ss:mmmAM (or PM) (Default + millisec)
110, mm-dd-yyyy (USA)
111, yyyy/mm/dd (Japan)
112, yyyymmdd (ISO)
113, dd mon yyyy hh:mi:ss:mmm (Europe (24 hour clock)>)
114, hh:mi:ss:mmm (24 hour clock)
120, yyyy-mm-dd hh:mi:ss (ODBC canonical (24 hour clock))
121, yyyy-mm-dd hh:mi:ss.mmm (ODBC canonical (24 hour clock))
126, yyyy-mm-ddThh:mi:ss.mmm (ISO8601)
127, yyyy-mm-ddThh:mi:ss.mmmZ (ISO8601 (with time zone Z))
130, dd mon yyyy hh:mi:ss:mmmAM (Hijiri)
131, dd/mm/yy hh:mi:ss:mmmAM (Hijiri)
*/
/**************************
FORMAT and TRY_FORMAT
**************************/
/*
FORMAT() allows you to format the results, such as changing date types or adding comma separators,
for viewing in a result set, though you'll take a sizeable performance hit. It's usually best to
leave the formatting to the client application that will be receiving the data, rather than using
the overhead to convert the data within SQL Server.
*/
-- main query which returns results that are not formatted
SELECT stationname, date, rides
FROM CTARidershipStationDaily
ORDER BY rides DESC, stationname, date
/* Results:
stationname date rides
Belmont-North Main 2015-06-28 00:00:00.000 36323
Belmont-North Main 2012-06-24 00:00:00.000 36017
Lake/State 2016-11-04 00:00:00.000 35845
Belmont-North Main 2011-06-26 00:00:00.000 33795
Addison-North Main 2016-10-29 00:00:00.000 33615
*/
-- example using FORMAT to add a comma separator to the rides column
SELECT stationname, date, FORMAT(rides,'###,###,###') AS 'No Rides'
FROM CTARidershipStationDaily
ORDER BY rides DESC, stationname, date
/* Results:
stationname date No Rides
Belmont-North Main 2015-06-28 00:00:00.000 36,323
Belmont-North Main 2012-06-24 00:00:00.000 36,017
Lake/State 2016-11-04 00:00:00.000 35,845
Belmont-North Main 2011-06-26 00:00:00.000 33,795
Addison-North Main 2016-10-29 00:00:00.000 33,615
*/
-- example showing how you can place any text in the string
SELECT stationname, date, FORMAT(rides,'### <> ### <> ###') AS 'No Rides'
FROM CTARidershipStationDaily
ORDER BY rides DESC, stationname, date
/* Results:
stationname date No Rides
Belmont-North Main 2015-06-28 00:00:00.000 <> 36 <> 323
Belmont-North Main 2012-06-24 00:00:00.000 <> 36 <> 017
Lake/State 2016-11-04 00:00:00.000 <> 35 <> 845
Belmont-North Main 2011-06-26 00:00:00.000 <> 33 <> 795
Addison-North Main 2016-10-29 00:00:00.000 <> 33 <> 615
*/
-- example of formatting the date column as a date with no timestamp. The 'd' tells it to show the date without a leading zero, and 'en-us' tells it to format using the US MM/DD/YYYY format (instead of the usual YYYY/MM/DD)
SELECT stationname, FORMAT(date, 'd', 'en-us') AS 'Date Observed', rides
FROM CTARidershipStationDaily
ORDER BY rides DESC, stationname, date
/* Results:
stationname Date Observed rides
Belmont-North Main 6/28/2015 36323
Belmont-North Main 6/24/2012 36017
Lake/State 11/4/2016 35845
Belmont-North Main 6/26/2011 33795
Addison-North Main 10/29/2016 33615
*/
-- example of spelling out the date in English, like 'Sunday, June 6, 2015' - 'dddd' tells it to use the full date name (ie 'Sunday'), MMMM tells it to spell out the month ('June'), 'd' tells it to include the date with no leading zero, and 'YYYY' tells it to include the year.
SELECT stationname, FORMAT(date, 'dddd, MMMM d, yyyy') AS 'Date Observed', rides
FROM CTARidershipStationDaily
ORDER BY rides DESC, stationname, date
/* Results:
stationname Date Observed rides
Belmont-North Main Sunday, June 28, 2015 36323
Belmont-North Main Sunday, June 24, 2012 36017
Lake/State Friday, November 4, 2016 35845
Belmont-North Main Sunday, June 26, 2011 33795
Addison-North Main Saturday, October 29, 2016 33615
*/
-- you can also use CONVERT to VARCHAR type and code 1 which will place comma separators into a number (for example, the number 23534.12000 will be returned as 23,534.00)
SELECT *, CONVERT(varchar(20), Amount, 1) AS 'Converted Amount' FROM windowTester
ORDER BY Territory, SalesPerson
/* Results:
SalesPerson Territory SalesYear Amount Converted Amount
12 1 2011 89333.20 89,333.20
12 1 2012 2393.30 2,393.30
12 1 2012 373525.98 373,525.98
*/
/**************************
PARSE AND TRY_PARSE
**************************/
/*
Parse is an alternative to CONVERT when you want to parse a character string input to a target type,
but instead of using format codes like '101', it uses a more user-friendly .NET culture name.
However this is very slow, so normally use should use CONVERT instead.
*/
SELECT PARSE('01/03/2017' AS DATE USING 'en-US')
以上是关于sql 转换数据,CAST,CONVERT,TRY_CAST,TRY_CONVERT,FORMAT,PARSE.sql的主要内容,如果未能解决你的问题,请参考以下文章
SQL SERVER中强制类型转换cast和convert的区别
SQL SERVER中强制类型转换cast和convert的区别