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 OverflowAll-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
Can someone explain CAST() to me?
Cast vs convert?
How do I convert these values (currently String) into INT (but with a catch)
How do I parse a String into java.sql.Date format?
Can't your SQL server accept strings for dates? The ones I've used can, so I just do something like:
String sqlDate = new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("dd-MM-yyyy").parse(startDate));... and pass sqlDate to the parametrized query. Like konrad mentioned, lowercase 'mm' is for minutes, and uppercase 'MM' is for month, so I think that's where your problem was.
More on reddit.comVideos
What is its function? Why do I sometimes need to use it (for timestamps mainly) and sometimes I dont?