PARSE() returns the result of an expression, translated to the requested data type.
It takes three arguments, first parameter is stringvalue which will take nvarchar(4000) value. Second parameter is datatype which representing datatype requested for the result and third is culture which is optional which take a current language of session as default. passed string value must be valid representation for requested datatype otherwise PARSE() will throw error.
But here are some limitations of the datatype which can used for requested result.
In first example it parsing string value to datetime and bigint dataype and in second example parsing string value to numeric and datetime datatype and you can see the resulted output also.
SELECT PARSE('Monday, 13 December 2010' AS DATETIME)
-- Valid parse
-- Output : 2010-12-13 00:00:00.000
SELECT PARSE('Monday, 13 December 2010' AS BIGINT)
-- Not valid parse
--Msg 9819, Level 16, State 1, Line 1
--Error converting string value 'Monday, 13 December 2010' into data type bigint using culture ''.
SELECT PARSE('18' AS NUMERIC(18,2))
-- Valid parse
-- 18.00
SELECT PARSE('18' AS DATETIME)
-- Not valid parse
--Msg 9819, Level 16, State 1, Line 1
--Error converting string value '18' into data type datetime using culture ''.
Read More..
 
[0 clicks]
Published under:
SQL Server Tips · · · ·