Need to identify number that is higher than a given date - sql

I have a date column in a table. The date column is in varchar. I want to identify a particular date range from that date column. My query is like this:
SELECT *
FROM [003 AccptReg].[dbo].[SysData1]
WHERE [RegDate_Sys] > '18 jul 2013'
But the result is not giving accurate result, i.e. it gives dates which are prior of 18 jul 2013.
Is there any thing wrong I am doing?

For date column, you should compare as DATE
select * from [003 AccptReg].[dbo].[SysData1]
where CAST([RegDate_Sys] AS DATE) > CAST('18 jul 2013' AS DATE)

The problem is that you have the date as a varchar, and doesn't convert it to a date when you are doing the comparison. The database doesn't know that you see the data as dates, and will simply compare them as strings, so for example '2 jan 1736' will be larger than '18 jul 2013' because 2 comes after 1.
The best would be if you could store the data as datetime values (or date), then you don't need to do the conversion when you compare the values, which would give better performance.
If that's not possible, do the conversion in the query:
select * from [003 AccptReg].[dbo].[SysData1]
where convert(datetime, [RegDate_Sys], 106) > '18 jul 2013'
Depending on the settings on the server, you might also need to convert '18 jul 2013' in the same way for the database to understand it correctly as a date.

Convert the date to datetime format and then compare:
select * from [003 AccptReg].[dbo].[SysData1]
where convert(datetime,[RegDate_Sys]) >convert(datetime,'18 jul 2013')

Related

Updating active record relation time column results in 1 day difference than expected

I'm trying to bulk edit a datetime column on some records, leaving the time alone and only adjusting the date. The below code works unless the time is > 1800. When the record's time is > 1800, the updated date is 1 day less than desired (Wed, 27 Jan 2021 14:25:26 CST -06:00 becomes Wed, 26 Jan 2021 14:25:26 CST -06:00). We're in Central Time (Dallas, TX), so I'd assume it's an issue with timezones, and I think the issue is happening in the process of structuring the new datetime.
I've tried this with DATETIMEFROMPARTS and the 2 options below. Same issue every time.
This is in a Rails 5 app with sql server for the db. target_date is formatted yyyy-mm-dd.
products_relation.update_all("
date_column = DATEADD(
day,
DATEDIFF(dd, date_column, '#{target_date}'),
date_column
)
")
products_relation.update_all("
date_column = CONVERT(
datetime,
CONCAT('#{target_date}', ' ', CONVERT(CHAR(8), date_column, 108))
)
")

Conversion of datetime to nvarchar(8) in T-SQL

I'm trying to convert a datetime column to ISO format, as yyyymmdd. For example, I would like to be able to convert '13 dec 2018' to '20181213'.
According to Microsoft's T-SQL Docs, I should be able to do this using
convert(nvarchar(8), '13 dec 2018', 112)
however this doesn't work - I get the result '13 dec 2', which looks to be nothing more than the original string cut down to 8 characters.
I have also tried using
convert(datetime, '13 dec 2018', 112)
which gives me the result of 'Dec 13 2018 12:00AM' - again, nothing like what the function is supposed to produce.
What I am doing wrong? I could solve the problem easily enough using datepart() and concatenated strings, but I'd rather use the more elegant approach if possible.
Combine them:
convert(nvarchar(8), convert(datetime, '13 dec 2018'), 112)
I don't recommend using the format 112 for the first conversion, because it is misleading. SQL Server is very good at converting without a format. If you do use one the appropriate one is 106.
I figured it out - because I was entering the date as a string, and not converting it to a datetime value first, it was treating my date as if it were a string. When I handle it this way:
convert(nvarchar(8), cast('13 dec 2018' as datetime), 112)
I get the expected result, '20181213'.
select convert(nvarchar(8), cast(GETDATE() as datetime), 112) as TodayDate
YYYYMMDD format can obtain by datetime to varchar format.
select convert(varchar(8),convert(datetime, '13 dec 2018'),112)
The above query provides the required result

Get data that is newer than given date

I am using Oracle
I want to see the result my data, where the date is after Jun 25th 2017. But I am seeing results from data that is older than the 25th Jun 2017. Some of it go all the way back to 1998 etc.
Here is my code:
select DATE_OF_LAST_CHANGE_OF_IMMU
from recipient_treatment
where DATE_OF_LAST_CHANGE_OF_IMMU > TO_DATE('25-JUN-2017', 'DD-MON-YYYY')
;
Is there something in my code that isn't right for what I wish to see?
The column: DATE_OF_LAST_CHANGE_OF_IMMU is of type date in the table
Please use the trunc command while filtering the date field as specified below
select DATE_OF_LAST_CHANGE_OF_IMMU
from recipient_treatment
where TRUNC (DATE_OF_LAST_CHANGE_OF_IMMU) > TO_DATE('25-JUN-2017', 'DD-MON-YYYY')
;

Convert text field to Date/time field ms access query

Hi guys can i ask if it's possible to convert a text field to a date/time field?
for example i have a field called Month and the field is a short text data type.
Here is an example:
Month
Sep 2016
Nov 2016
Dec 2016
is it possible that i could convert this one to a date time?
Try this
DateValue("1 " & Month)
I'm assuming you want to put the first of the month in the date.
Output should be
9/1/2016
11/1/2016
12/1/2016
in your case
SELECT convert(datetime, 'Oct 2015 12', 0) or
SELECT convert(datetime, 'Oct 2015', 0) or
SELECT convert(datetime, 'Oct 2015')
It's so easy that it can't be simpler:
FirstOfMonth: CDate([Month])
The values will be primo of [Month].

Convert string into date in SQL Server

Conversion failed when converting date and/or time from character string.
I'm getting the above error when running this statement in SQL Server:
SELECT CONVERT(datetime, 'Fri, 15 Jan 2016 17:30:05 GMT')
Actually I want to insert same string format in Datetime column
As suggested by Tim Biegeleisen, that string needs to be processed to be converted. In order to convert it you need to strip of the day (Fri,) and the GMT timezone at the end, for example:
DECLARE #date varchar(50) = 'Fri, 15 Jan 2016 17:30:05 GMT'
SELECT CONVERT(DATETIME, SUBSTRING(#date, 5, LEN(#date) - 8), 113)
This solution does strip the timezone information, have a look at this post if you want to convert it back to UTC.
If you want to insert the string 'Fri, 15 Jan 2016 17:30:05 GMT' into datetime column then you need to remove Fri, and GMT from the string before inserting.
SELECT CAST(substring(#str_date,5,len(#str_date)-8) as datetime)
DEMO