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())
Related
Trying to select items from a database where the date that is being entered is converted to a different timezone.
In this case, the date is entered into the server in UTC time, and I've got a select query offsetting this date to meet the NZST time zone:
Code attempted
I'm also trying to select the items where the date in this 'NZSTdate' column match with today's date, and have had no such luck casting both items in the date format in a WHERE clause.
Just says the conversion failed from a character string.
SELECT *, convert(datetime, switchoffset(fulldate, datepart(tzoffset, fulldate AT TIME ZONE 'New Zealand Standard Time'))) AS 'NZSTdate'
FROM [Analytics].[dbo].[Call logs]
where cast('NZSTdate' as date) = cast(getdate() as date)
order by 'fulldate' desc
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I think you have a few things going on here.
"cast('NZSTdate') as date" looks suspicious. You almost certainly don't want the apostrophes around the field name like that.
Similarly, you don't want "fulldate" in apostrophes either.
Does this get you closer to what you're attempting?
create table #kl (dt datetimeoffset )
insert #kl values (getdate())
-- changing timezone
select switchoffset(dt, '-04:00')
from #kl
-- getting rid of time time component of dt and getdate()
where convert(date, dt) = convert(date, getdate() )
I'm trying to change the date format of data in column, that is had been inserted in this format
dd/mm/yyyy. I wanna change to this format yyyy-MM-dd in select statement.
I've tried by this query :
select CONVERT(nvarchar,cast(date1 as DATE),23) from Table1
On first two second I getting the result correctly then this Error appear:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
If you have inserted the value as a string, then you need to convert from that format. For the one you specify, you can try:
convert(date, datecol, 103)
Next, if the column is already a date, then your SQL Server settings might be controlling how the data is displayed. In this case, you can add a computed column for the format you want:
alter table t add datecol_yyyymmdd as (convert(varchar(10), datecol, 120));
If the column is a string and you want to convert the format, first check to see if all the values are as you expect. To return bad values:
select datecol
from t
where try_convert(date, datecol, 103) is null and datecol is not null;
If all are fine, then you can use:
update t
set datecol = convert(varchar(10), convert(date, datecol, 103), 120);
You can then alter the type to be a date.
I am trying to convert a varchar to date using the below code.
SELECT CAST('14/08/2018' as date) --This code does not work
SELECT CAST('09/08/2018' as date) --This code works
It appears that when the day part of the date gets to '13' that is where it starts breaking.Is there a logical explanation for this?
The error given is :
Msg 241, Level 16, State 1, Line 7670
Conversion failed when converting date and/or time from character string.
You should decide your date component before conversation :
I would considered date with style dd/mm/yyyy :
SELECT CONVERT(DATE, '14/08/2018', 103)
However, it seems SQL has set date mm/dd/yyyy.
If so, you can change it :
set dateformat dmy
The cause of your problem is that you have a mm/dd/yyyy format and the first value is the month. You will either need to swap the first and the second value. You can use convert for this purpose with option 101, which converts a mm/dd/yyyy to a mm/dd/yyyy:
select convert(DATE, '14/08/2018', 101);
This is the US standard and this converts your varchar to a DATE. Now, if you want to display this in a format of dd/mm/yyyy, then just do
select convert(varchar(10), convert(DATE, '14/08/2018', 101), 103);
I want to get the total sum from a the table from the previous day. I get an error
My code:
select
date_paid, sum(paid_amount) as amount
from
till1
where
date_paid = dateadd(day, datediff(day, 0, getdate()), 0)
group by
date_paid
I want to get a result something like this:
I get the following error:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value
Presumably, this is SQL Server. You can fix the syntax error by using try_convert():
select date_paid, sum(paid_amount) as amount
from till1
where try_convert(date, date_paid) = cast(getdate() as date)
group by date_paid;
However, the real solution is to fix the data. Dates should not be stored as strings.
need help. i just want to get past 3 months transaction.
SELECT *
FROM transaction
WHERE TransactionDate >= GETDATE()-90
transactionDate is a INT type with this format YYYYMMDD (ex. 20160812)
i got this error
Arithmetic overflow error converting expression to data type datetime.
any help will do
You can't convert an int directly to a date. However, you can convert a string in YYYYMMDD format. So, try this:
WHERE cast(cast(TransactionDate as varchar(255)) as date) >= GETDATE() - 90
Alternatively, you could do all the conversion on the current date side:
WHERE TransactionDate >= CONVERT(INT, CONVERT(VARCHAR(255), GETDATE() - 90, 112))
The advantage of this method is that the engine can use an index on TransactionDate.
That said, the right way to store the column is as a DATE. SQL Server has built-in date/time data types for a reason; there is a lot of direct support for them. Use the capabilities of the database.