Trying to display a combination of String MM + int DD + int YYYY in SQL Management Studio - sql

I am trying to display a combination of String MM + int DD + int YYYY
in SQL Management Studio. However, i encountered an error like this which says
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'November ' to data type int.
My codes are:
SELECT DATENAME(MM, Check_in_date) + ' ' + DAY(Check_in_date) + ' ' +
YEAR(Check_in_date)
FROM Book_Details
Afterwards, i tried casting the Check_in_date into a varchar but encountered an error as well. Any help will be appreciated

You are trying to concatenate a string and an integer.
You need to cast #ID as a string.
try:
SELECT DATENAME(MM, Check_in_date) + ' ' + CAST(DAY(Check_in_date) AS NVARCHAR(2)) + ' ' +
CAST(YEAR(Check_in_date) AS NVARCHAR(4))
FROM Book_Details
if you don't like the space, you may use LTRIM to remove the space
Refer : link
UPDATED on year part

Related

"Arithmetic overflow error converting expression to data type nvarchar."

I'm trying to do a comparison of data between 2 tables, where I need to join multiple columns as a composite key to get a unique identifier. I'm using a CTE and the code I have is:
WITH SuburbDataTest AS (
SELECT *
, CAST(Address AS NVARCHAR(100))+' ' +CAST(LivingAddress AS NVARCHAR(2))
+ ' ' + CAST(StartDate AS NVARCHAR(11))+ ' ' +CAST(AddressTypeId AS NVARCHAR(1))
+ ' ' +CAST(SuburbId AS NVARCHAR(1))AS SuburbDataTestColumn
FROM [mig].[ConsumerAddressMigration]
WHERE SuburbId is NOT NULL
)
SELECT *
FROM SuburbDataTest staging
WHERE SuburbDataTestColumn IN (
SELECT Address+' ' +CAST(LivingAddress AS NVARCHAR(2))+ ' '+CAST(StartDate AS NVARCHAR(11))
+ ' ' +CAST(AddressTypeId AS NVARCHAR(1))+ ' ' +CAST(SuburbId AS NVARCHAR(1)) AS SuburbDataTestColumn
FROM [dbo].[tblConsumerAddress]
)
Unfortunately I'm getting
Arithmetic overflow error converting expression to data type nvarchar.
Any ideas?
This happens when you are converting a number to a string -- and the string is not big enough. I would guess this is the problem:
CAST(SuburbId AS NVARCHAR(1))
If SuburbId is a number larger than 9, then this will generate an error. Or, for that matter if the value is negative you'll get the same error as well.

SQL Server: using Case statement in Where clause creates error

I have a dynamic procedure where an input variable can either be a date with the corresponding column being formatted as datetime OR a string with the corresponding columns being formatted as nvarchar.
If the input is a date then this would look like yyyy-mm-dd .
To cover this I tried to add the following to my Where clause but this creates the below error (if I remove this line then the rest of the procedure works as intended):
#searchTerm nvarchar(256) = ''
-- ...
WHERE CASE WHEN ''' + #searchCategory + ''' <> ''dateX'' THEN
(A.' + #searchCategory + ' LIKE ''%' + #searchTerm + '%'')
ELSE
(A.dateX = ''' + CAST(#searchTerm AS DATETIME) + ''')
END
-- ...
Error:
Conversion failed when converting date and/or time from character string.
Can someone tell me what I have to change here to make this work ?
Many thanks in advance, Mike.
You can't put a datetime value into a string. First cast as datetime to get it into the format you want then cast to a Varchar so it can be part of your dynamic string.
(A.dateX = ''' + CAST(CAST(#searchTerm AS DATETIME) AS VARCHAR(20)) + ''')
Or if #searchTerm is already in the format you want then you don't need to cast it
(A.dateX = #searchTerm)

How to concatenate month number into table name

I like to concatenate the month number with a text to build table names. For example, I am trying to retreive data for May 2013, I would like to select from webproxylog5.
The following script
select *
from webproxylog + '' + cast(month(dateadd(m,-2,getdate())) as varchar(2)) + ''
will result in the following error messsage:
Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near '+'.
What is wrong with this syntax?
Thank you,
Seyed
you would need to build dynamic sql for that,
something like
declare #sql varchar(200)
set #sql= 'select * from webproxylog + ' + cast(month(dateadd(m,-2,getdate())) as varchar(2))
exec(#sql)

The best way to add a concatenated string of multiple fields to a stored procedure

I have 14 datetime fields in my database. Ive been asked to return a single field of the 14 dates as one string field. Right now my stored proc looks kinda like this:
convert(varchar, [DTMON_F],108) as mondayFrom,
convert(varchar,[DTMON_T],108) as mondayTo,
convert(varchar,[DTTUES_F],108) as tuesdayFrom,
convert(varchar,[DTTUES_T],108) as tuesdayTo,
I want to have a single field called extendedDetails that is in the format HHmm - HHmm, HHmm - HHmm
This returns for example "10:20:00" so I'll have to somehow cut this to look like "1020" and then somehow concat all of them.
convert(varchar, [DTMON_F],108) as mondayFrom
SELECT
REPLACE
(
convert(char(5), [DTMON_F],108) + ' - ' +
convert(char(5),[DTMON_T],108) + ' - ' +
convert(char(5),[DTTUES_F],108) + ' - ' +
convert(char(5),[DTTUES_T],108) + ' - ' +
...
, ':', '') AS whatever
FROM MYTABLE

Having some Dynamic SQL issues with INT type

Hello Im using SQL2000 so I build a Dynamic Query and in the last case I have this :
IF (#apepac is not null and #nompac is not null and #month is not null )
SELECT #DynaSQL_1= #DynaSQL_1 + ' AND PACIENTE.apellidos like ''' + #apepac + '%'''+
' AND PACIENTE.nombres like ''' + #nompac + '%'''+
' AND DATENAME(MONTH,honorariotecnologo.fechaestudio) = ''' + #month +'''' +
' AND YEAR(honorariotecnologo.fechaestudio) = '+#year+''
so the parameter #year is declared in this way :
DECLARE #year int,
and the error I get from SQL output is :
Msg 245, Level 16, State 1, Line syntax
43Error to convert the nvarchar value '
What could be wrong?
Thanks!
By the way, Why if the parameter is declared as INT, on the body query it must have to be casted / converted? ...
You have to cast or convert the INT to a NVARCHAR. Google CAST CONVERT TSQL.
You need to cast your #Year as a character value.
Try this:
' AND YEAR(honorariotecnologo.fechaestudio) = ' + CAST(#year AS varchar(10))
You want this to take care of the conversion error...
' AND YEAR(honorariotecnologo.fechaestudio) = '+CAST(#year AS VARCHAR)
You want this if you want to add the single quote to the end of your string.
' AND YEAR(honorariotecnologo.fechaestudio) = '+CAST(#year AS VARCHAR) + ''''