All-caps is literal text, lower-case is something into which you should interpolate a value:
CAST(expression AS CHAR)
-- or:
CAST(expression AS VARCHAR)
You can optionally specify a length.
Unfortunately, it's a bit harder with datetimes, if you want to format it in any way that's different from the default representation.
My info came from the MSDN site. I think you'll have to read that site, and others, carefully and play around with it a bit, but hopefully the CAST function will be a good start.
Good luck!
Answer from Platinum Azure on Stack Overflowsql server 2005 - tsql : best way to cast variables to string type? - Stack Overflow
Cast vs convert?
Can someone explain CAST() to me?
How do I convert these values (currently String) into INT (but with a catch)
Videos
All-caps is literal text, lower-case is something into which you should interpolate a value:
CAST(expression AS CHAR)
-- or:
CAST(expression AS VARCHAR)
You can optionally specify a length.
Unfortunately, it's a bit harder with datetimes, if you want to format it in any way that's different from the default representation.
My info came from the MSDN site. I think you'll have to read that site, and others, carefully and play around with it a bit, but hopefully the CAST function will be a good start.
Good luck!
you need to convert/cast it each time. I made a function to use:
CREATE FUNCTION QuoteNull
(
@InputStr varchar(8000) --value to force to string
)
RETURNS
varchar(8000)
AS
BEGIN
RETURN COALESCE(''''+@InputStr+'''','null')
END
it puts single quotes around the value or just the word null if it is null, but you can customize it as necessary.
here is a version that handles formatting dates automatically:
CREATE FUNCTION QuoteNull
(
@InputStr sql_variant --value to force to string
)
RETURNS
varchar(8000)
AS
BEGIN
DECLARE @String varchar(8000)
SET @String=COALESCE(''''+ CASE SQL_VARIANT_PROPERTY(@InputStr,'BaseType')
WHEN 'datetime' THEN CONVERT(varchar(23),@InputStr,121)
ELSE CONVERT(varchar(8000),@InputStr)
END
+'''','null')
RETURN @String
END