Why I cannot use Convert() column in a where clause? - sql

The following SQL
select creationdate as dt
from dbo.Posts2
where dt > '2013-06-31'
gives error when running:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'dt'.
Update
this works:
Declare #date as varchar(30)
set #date='2008-07-31 21:42:52.667'
select convert(datetime,#date, 101) as [DateColumn]
but this failed:
select creationdate from dbo.Posts2
where convert(datetime, creationdate, 101) > '2013-06-31'
The error is:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
Why??

You can't use aliases in the where clause.
Just write
where convert(datetime, creationdate) > '2013-06-31'
Edit about the datetime/varchar conversion error:
You are trying to compare to a date that doesn't exist: June only has 30 days, so '2013-06-31' is not a date for SQL Server.

Related

show data with shipmentdate bigger than today

i am trying to pulling data for all shipments with shipmentdate greater than todays date. However, I cant figure out an easy conversion of the format of the nvarchar, i get a out of range value error when trying to run this:
select *
from dbo.BAS_CT_RAW_ARCHIVE_TBL
where SHIPMENTDATE > GETDATE()
Msg 242, Level 16, State 3, Line 1 The conversion of a nvarchar data
type to a datetime data type resulted in an out-of-range value.
Try:
select *
from dbo.BAS_CT_RAW_ARCHIVE_TBL
where convert(date, SHIPMENTDATE, 103) > GETDATE()
To see how to use convert with non-standard dates see this.
Further consideration: use proper datatypes for columns, i.e. don't store dates as strings, but as date datatype - it will prevent you from having such problems.
according to your data format that got from comment below should work
select * from dbo.BAS_CT_RAW_ARCHIVE_TBL
where CONVERT(date, SHIPMENTDATE, 103) > convert(date, GETDATE())

I'm having date conversion problems with the 3rd column in the script below

Select c1.CDS_Date, c2.CDS_Date, Dateadd(day, -1 , c2.CDS_Date)
from cte as c1 left join
cte as c2
on c1.[Local Patient Identifier] = c2.[Local Patient Identifier] and
c1.JoinKey = c2.JoinKey - 1
Can I get come help please?
I got no issues with c1.CDS_Date and c2.CDS_Date.
I receive the following error message as soon as I include the 3rd column:
(24 rows affected)
Msg 242, Level 16, State 3, Line 50
The conversion of a varchar data type to a datetime data type resulted in an --out-of-range value.
That is because your CDS_Date is not of date/datetime type but is a string.
Here is an example:
declare #t table (CDS_Date varchar(100));
insert into #t values ('28/10/2017');
set language British;
select CDS_Date, dateadd(day, -1, CDS_Date) as dt
from #t;
-------
--CDS_Date dt
--28/10/2017 2017-10-27 00:00:00.000
set language us_english;
select CDS_Date, dateadd(day, -1, CDS_Date) as dt
from #t;
--Msg 242, Level 16, State 3, Line 40
--The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
This example shows you that not every string can be converted to date EVEN IF it seems to you that it represents a valid data. This is because server expects a date that you pass as a string to be in the certain format that depends on session language.
To be independent of session language you shoud pass your date string using convert function and specifying the format of your date.
In my example I can fix the issue converting to date with style 103:
select CDS_Date, dateadd(day, -1, convert(date, CDS_Date, 103)) as dt
from #t;
----
--CDS_Date dt
--28/10/2017 2017-10-27
Now it gives me the correct result in both languages just because it knows that my input strings are of 103 format, i.e. dd/mm/yyyy

Int to Date in SQL SERVER 2000

How to convert an int column ,Birthdate( sample value 20090301) to a date column in the format(01/03/2009)
Sql Server 2000
I was trying to convert some thing like below
select * from tabl
where
cast(dob as datetime)>='01/03/2009'
which gives an error
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.
SQL Server doesn't allow you to cast int -> datetime directly, but if you're sure you can cast it to nvarchar first and then cast it to date.
For your question here's an example
declare #test int = 20090301
select CONVERT(datetime, CAST(#test as nvarchar), 112)
112 is the format you mentioned, here's the list of all possible formats for Convert function.
try this !
select * from tabl
where
cast(convert(char(8),dob, 112) as date)>='2009-03-01'

trying to get age from date of birth

SELECT [ID_KUNDNR]
,CASE
WHEN DA_FODELSEAR IS NULL THEN 0
WHEN dateadd(year, datediff (year, DA_FODELSEAR, getdate()), DA_FODELSEAR) > getdate() THEN datediff (year, DA_FODELSEAR, getdate()) - 1
ELSE datediff (year, DA_FODELSEAR, getdate())
END As Age
,[Catalog]
FROM Table
I get
Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting
expression to data type datetime.
Any solutions?
I'm just wondering why you are using case. Try the below and see whether it works.
SELECT [ID_KUNDNR] ,
datediff (year, 0, (getdate()-DA_FODELSEAR)) as Age,
Catalog
From Table
But again are you sure all values of DA_FODELSEAR is convertable to datetime?
If you have trouble try this.
SELECT [ID_KUNDNR] ,
Case When ISDATE(DA_FODELSEAR) = 0 Then 0 Else
datediff (year, 0, (getdate()-DA_FODELSEAR))
End as Age,
Catalog
From Table
Incase you want to know, which rows are causing this conversion problem select the table with 'where IsDATE(DA_FODELSEAR) = 0'
You have to make sure that DA_FODELSEAR is in correct datetime format. You can do it by using CAST(DA_FODELSEAR as DATETIME) and check.
I think that your problem lies here dateadd(year, datediff(year, DA_FODELSEAR, getdate()), DA_FODELSEAR). You have a value in your DA_FODELSEAR that is not in a DATETIME format. I would run a SELECT statement to find the offending value. You are checking for NULL but what about a blank space?

convert nchar(20) field to a datetime data type in sql server 2005

I need to select from a table and filter on dates greater than a
specified date.
The problem I'm having is that the dates are stored as nchar(20) and I
can't seem to get it converted to date in the where clause.
SELECT CONVERT(DATETIME,log_time,20) from dbo.logs
where CONVERT(DATETIME,log_time,20) > '10/20/2008'
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.
This should work.
select convert(datetime, cast(rtrim(log_time), nvarchar)) from dbo.logs
where convert(datetime, cast(rtrim(log_time), nvarchar)) > '10/20/2008'
I talked to our SQL Server DBA. He said the problem was with bad data. He said on some the dates the m was left off of the am or pm. He wrote me a view where he handles the problem for me and now I can select from the view and the date is a datetime data type also.
Thanks for everyone's help.
Billy
try this:
select convert(datetime, cast(trim(log_time), nvarchar)) from dbo.logs
where convert(datetime, cast(trim(log_time), nvarchar)) > '10/20/2008'
I think the trick is that you need to trim the string and cast to nvarchar
Msg 195, Level 15, State 10, Line 1
'trim' is not a recognized built-in function name.
Msg 1035, Level 15, State 10, Line 1
Incorrect syntax near 'cast', expected 'AS'.
Try
select convert(datetime, cast(rtrim(log_time) AS nvarchar)) from dbo.logswhere convert(datetime, cast(rtrim(log_time) AS nvarchar)) > '10/20/2008'
or simply
select CAST(log_time AS DATETIME) from dbo.logs
where CAST(log_time AS DATETIME) > '10/20/2008'