I've written following code & im trying to search this table for rows between a specific period:
SELECT 'RWH' + P.Hospital_Number AS MRN,
P.Date_of_birth,
pd.Clinic_date AS Date_Seen,
pd.Clinic_type_No
FROM (Patient_diabetes AS pd
INNER JOIN Patients AS p ON pd.Patient_No = p.Patient_No)
INNER JOIN Staff AS s ON pd.Seen_by1_No = s.Staff_No
WHERE (((pd.Clinic_type_No)=241))
--AND hospital_number like '%63028%'
AND CONVERT(datetime,pd.Clinic_Date,121) BETWEEN '02/04/2015' AND '24/09/2015'
ORDER BY p.Date_of_birth,
pd.Clinic_date DESC
But I get the following error, any ideas how to fix this?:
Msg 242, Level 16, State 3, Line 2 The conversion of a char data type
to a datetime data type resulted in an out-of-range datetime value.
I suggest you to change:
AND CONVERT(datetime,pd.Clinic_Date,121) BETWEEN '02/04/2015' AND '24/09/2015'
To this:
AND CONVERT(date,pd.Clinic_Date,121) BETWEEN '2015-04-02' AND '2015-09-24'
I think the problem here in the BETWEEN '02/04/2015' AND '24/09/2015'
By default '24/09/2015' converts into "mm/dd/yyyy" format data. So there is no 24th month.
Try to explicitly point the format of string data:
BETWEEN CONVERT(datetime,'02/04/2015',103)
AND CONVERT(datetime,'24/09/2015',103)
Related
I am trying to create a view, but I keep getting errors.
Can anyone help me with this error?
Msg 241, Level 16, State 1, Line 9
Conversion failed when converting date and/or time from character string
Code:
CREATE VIEW RecentAlbums
AS
SELECT
Artists.ArtistName, Albums.AlbumName, Albums.ReleaseDate, Genre.Genre
FROM
Albums
INNER JOIN
Artists ON Albums.ArtistId = Artists.ArtistId
INNER JOIN
Genre ON Albums.GenreId = Genre.GenreId
WHERE
(Artists.ArtistName > DATEADD(year, - 10, GETDATE()));
SELECT * FROM RecentAlbums;
Regards
because dateadd returns datetime and you try to use in where different type columns.
you can see below article about Dateadd to understand how it works.
http://www.codefolders.com/articles/sql/date-and-time-functions/dateadd().html
I am very new to Teradata and SQL in general. I need to create a table by combining data from three tables. I was able to successfully join two of them. I am not able to write the joining condition for the third table properly. Here is the code:
select s.cola, s.colb,
t.colc, t.cold,
u.cole, u.colf, u.colg, u.colh, u.coli, u.colj, u.colk, u.coll
from table1 s
inner join table2 t
on s.colb = t.colc
inner join table3 u
on t.cold = cast(u.colm as decimal)
order by 3
where substr(cast(s.cola as varchar(10)),6,2) = 11 and substr(cast(s.cola as varchar(10)),1,4) = 2017 and substr(cast(s.cola as varchar(10)),9,2) between 06 and 10
The error I am getting is:
[Teradata Database] [2620] The format or data contains a bad character.
I think the problem is with the line: on t.cold = cast(u.colm as decimal). The u.colm is of type VARCHAR(50) while t.cold is of type DECIMAL(10, 0). I believe I have casted it properly. Please help.Thanks in advance.
There's some bad data in u.colm.
Depending on your Teradata release you can check it using
WHERE u.colm > '' AND TRYCAST(u.colm as decimal(10,0)) ISNULL
or
WHERE u.colm > '' AND TO_NUMBER(u.colm) IS NULL
You can also use those in the join-condition, e.g.
on t.cold = trycast(u.colm as decimal(10,0))
Don't forget to add the precision of the decimal, as it defaults to (5,0).
Your WHERE_condition is strange, what's the datatype of s.cola?
Seems it's a string with a date yyyy-mm-dd in it. Try
WHERE trycast(s.cola as date) between date '2017-11-06' and date '2017-11-10'
Finally the ORDER BY should be placed after WHERE.
I have a query like this
select
t.tiid, t.employeeid, t.remarks,
dd.DocID, dd.Document, dd.DocuName
from
ti t
inner join
History cth on cth.tiid = t.tiid
inner join
Downloads dd on dd.DocID = cth.DocID
My data in table is like this
History:
DocID DocuName
1,2 abc.dox,def.docx
Downloads
DocID DocuName document
1 abc.docx x3400000efg..
2 def.docx xc445560000...
but when I execute this query, it shows an error:
Conversion failed when converting the varchar value '1,2' to data type int.
The DocID of history is multiple DocID had been combined with comma, So you can not compare the value directly( One value vs Multiple values).
You can check whether the multiple values contain the specify value use CHARINDEX.
To make sure complete matched of sub string,need a delimiter to indicate a single value, otherwise can get wrong result.
For Eample:
CHARINDEX('1,','12,2,3') will be 1, but in fact, there is no 1 in the string.
select
t.tiid,
t.employeeid,
t.remarks,
dd.DocID,
dd.Document,
dd.DocuName
from ti t
inner join History cth on cth.tiid=t.tiid
inner join Downloads dd on CHARINDEX(','+LTRIM(dd.DocID)+',',','+cth.DocID+',')>0
As the error says, you are trying to equate a string with int.You need to convert the int DocID as string and check if it's present in the comma-separated DocID .Something like
SELECT t.tiid,
t.employeeid,
t.remarks,
dd.DocID,
dd.Document,
dd.DocuName
FROM ti t
INNER JOIN History cth ON cth.tiid=t.tiid
INNER JOIN Downloads dd ON CHARINDEX(',' + CAST(dd.DocID AS VARCHAR(10)) + ',',',' + cth.DocID + ',')>0
I have a usecase where I want to take date of one collumn (2014/07/07) and time from another collumn of other table (8:00:00) but it is showing error while converting it.
Msg 8115, Level 16, State 2, Line 5
Arithmetic overflow error converting expression to data type datetime.
below is the query I used
select convert(datetime,convert(nvarchar(16),convert(nvarchar(8),r.eta)+' '+convert(nvarchar(7),m.[msps code time]))) from rawdata_master r join msps_port_code m on r.rkst=m.[gsis to port] where r.eta not like ' '
and m.[msps code time] not like '#N/A'
the second collumn from which I am taking time part have value like '#N/A'. Please help me out.
Found the answer . change from nvarchar(8) to nvarchar(9)
select convert(datetime,convert(nvarchar(18),convert(nvarchar(9),r.eta)+'
'+convert(nvarchar(9),m.[msps code time]))) convert(datetime,convert(nvarchar,
convert(nvarchar(18),convert(nvarchar(9),r.eta))+' '+convert(nvarchar(9),
m.[msps code time]))) from rawdata_master r join msps_port_code m on r.rkst=
m.[gsis to port] where m.[msps code time] not in ('#N/A','#NAME?','#REF!')
I have a trigger which executes after a record is inserted into a table. The trigger basically, calls a function and passes values from the insert into it. However, while it works some times, I also keep getting the following error:
Msg 8115, Level 16, State 2, Procedure UpdateNabersRating, Line 17
Arithmetic overflow error converting expression to data type int.
The trigger is as follows:
UPDATE BMS_Snapshot SET
NABERS = dbo.SetmetricsNABERSCalculation(
60,
tbl.NetFloorArea,
tbl.ElectricityCumulative,
tbl.GasCumulative,
tbl.ElectricityCumulativeMax,
tbl.GasCumulativeMax,
tbl.ElectricityMaxTotal,
tbl.GasMaxTotal,
tbl.NaturalGasConversionCubicMetersToMJ,
tbl.SuburbId)
FROM
(SELECT
Snap.SnapshotId AS SnapshotId,
Snap.ElectricityCumulative AS ElectricityCumulative,
Snap.GasCumulative AS GasCumulative,
Building.NetFloorArea AS NetFloorArea,
Building.NABERSElecTotalMax AS ElectricityMaxTotal,
Building.NABERSGasTotalMax AS GasMaxTotal,
Building.NABERSExpiry As Expiry,
NABERSTarget.ElecCumulativeMax AS ElectricityCumulativeMax,
NABERSTarget.GasCumulativeMax AS GasCumulativeMax,
[State].NaturalGasConversionCubicMetersToMJ AS NaturalGasConversionCubicMetersToMJ,
Suburb.SuburbId AS SuburbId
FROM inserted AS Snap
INNER JOIN AST_Building AS Building ON Snap.BuildingId = Building.BuildingId
INNER JOIN ESD_Suburb AS Suburb ON Building.SuburbId = Suburb.SuburbId
INNER JOIN ESD_State AS [State] ON Suburb.StateId = [State].StateId
INNER JOIN AST_NABERSTarget NABERSTarget ON Snap.BuildingId = NABERSTarget.BuildingId AND
Snap.TimeStamp = NABERSTarget.Timestamp
/*Where Snap.SnapshotId IN (Select inserted.SnapshotId FROM inserted)*/) AS tbl
WHERE tbl.SnapshotId = BMS_Snapshot.SnapshotId AND BMS_Snapshot.Range = 1
I've been playing around with it for a while, but can't seem to put my finger on the problem, particularly given it works sometimes.
Changed, arguments on function to FLOAT, instead of INT for two of the parameters.