Find between with separated date fields (year,month,day) - sql

I have the following dates in my table in separated fields. How can I write a query to show the values between two dates.
For example: values between 2/1/2011 and 2/6/2011:
day month year value
--------------------------------------------------
2 6 2011 120
3 7 2011 130
5 5 2011 100
6 1 2011 50

As others have said, my first suggestion would be to use Date. Or if you need more detailed information than your example, Datetime or Timestamp with Time Zone.
But in case you actually have to work with this data, I think something like this should work, depending on your flavor of SQL:
SELECT value, CONVERT(DATE,CONCAT(CONCAT(CONCAT(CONCAT(day, "-"), month), "-"), year), 105) date FROM table_name where (2/1/2011) <= date and date <= (2/6/2011);
(with Oracle SQL, you can use to_date instead of convert and optionally use the || concatenation operator; with SQL server you have to use the + concatenation operator; with MySQL this should be right)
(2/1/2011) and (2/6/2011) could either be strings that you convert similar to the other convert, or inputted using a PreparedStatement or something like it as dates directly (this would be preferable).

I had the same scenario but with Month column displaying Month name . With slight modification on the given query i was able to fetch the data.
SELECT *
FROM Table_Name AS Tbl_Date
WHERE (Year * 10000 + DATEPART(mm, CAST(Month + Year AS DATETIME)) * 100 + 1
BETWEEN 20111101 AND 20121201)
Hope this will help

To convert to Date for easier comparisons without worrying about dmy or mdy, in a standard fashion:
DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0)))
So, something like this. The safest date format to use is yyyymmdd (especially with SQL Server)
SELECT
value,
DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0))) AS realdate
FROM Mytable_name
WHERE
'20110201' <= DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0)))
and
DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0))) <= '20110206'

if you are using oracle database then you can use TO_DATE and TO_CHAR functions to achive this target...
as follow-
select * from table_name
where to_date(day||month||year,'DDMMYYYY')
between &mindate and &maxdate
min date you can put 2-jan-2011 and max date as 2-jun-2011
I hope it should work for you :)

well i found the answer that i wanted thanks guys
SELECT Tbl_Date.Value,Tbl_Date.Year,Tbl_Date.Month,Tbl_Date.Day from Tbl_Date
where ((Tbl_Date.Year*10000)+(Tbl_Date.Month*100)+Tbl_Date.Day) between 110102 and 110602

Related

Specific Date but increase Year based on GETDATE()

I'm using SQL Server.
I have a specific date (in dd/mm/yyyy format) i.e. 06/04/2020
However, in a T-SQL View, it needs to be always 1 year from now i.e. if I run today it would return 06/04/2021. And if it executed in 2021 it would return 06/04/2022 - how would I do this?
So I can run the below:
SELECT CONVERT(DATE, DATEADD(year, 1, '06/04/2020'), 103) as MyDate;
Which will give me:
2021-06-04
However, how do I make it self-maintaining?
You can make it generic by extracting the year from GETDATE(), adding 1 and concatenating that to 06/04 (or the date as required), and then converting. For example:
SELECT CONVERT(DATE, CONCAT('06/04/', DATEPART(YEAR, GETDATE()) + 1), 103)
Output:
2021-04-06

Looking to convert integer as date SQL

Looking to convert the integer '43676' and '43683' to a date. It's supposed to be '30-07-2019' and '06-08-2019' respectively, but SQL converts it into a wrong date when using e.g. convert(datetime, '43683').
Appears to be the number of days past since 1900-01-01, but odly offset by 2 days..
IN MS SQL you could use
SELECT DATEADD(DAY, 43676-2, '1900-01-01')
OR
SELECT DATEADD(DAY, 43683, '1899-12-30')
Maybe try -
SELECT DATEADD(day, 43676, '1899-12-30')
Result:
2019-07-30 00:00:00.000

SQL date comparision in yyyy/mm/dd

Not being that great at sql, i've reached my limit.
I have a date in the yyyy/mm/dd format and i need to get all records "from a week ago"
I think i need some conversion stuff to be done cause this
d.date_begin >= DATEADD(day,-7, GETDATE())
is not working :), i'm TERRIBLE at convert and data type..
This will work if you want records from 7 days ago up to and including today's records
CAST(d.date_begin AS DATE) >= CAST(DATEADD(day,-7, GETDATE()) AS DATE)
where DATEDIFF(month,Your_date_column,getdate()) < 3
SQL server 2012 onwards, if date_begin is of datatype date
where d.date_begin >= cast(DATEADD(day,-7, GETDATE()) as date)
This will get anything in the last 7 days, regardless of time
You should store date/time values using native formats. Ok, sometimes we cannot. But you can easily convert your values to the correct format:
where cast(replace(d.date_begin, '/', '') as date) >= DATEADD(day, -7, GETDATE())
I should note that SQL Server is pretty good about conversions, so your initial code should not generate any errors -- unless you have unusual internationalization settings.
Or, actually, a better way to do this is to convert the current value to a string:
where d.date_begin >= format(dateadd(day, -7, getdate()), 'yyyy/MM/dd')
This is better because it is "sargable", meaning that SQL Server can use an index on the column if available.
SELECT * FROM tbl_name
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Don't manipulate d.date_begin. Making calculation on a column while comparing can give bad performance. You should manipulate getdate() to get the same format as d.date_begin. In this case it works because the format is yyyy/MM/dd - the comparison will give the same result as if both columns were date columns.
WHERE
d.date_begin >= convert(char(10),DATEADD(day,-7, GETDATE()), 111)

issues with showing date with time format in sql [duplicate]

This question already has answers here:
Best approach to remove time part of datetime in SQL Server
(23 answers)
Closed 8 years ago.
I am trying to show only date here and not the time but whatever i do still is showing the time like this format 4/4/2014 12:00:00 AM. I would like to remove the time portion of the date and only show the date. What am i doing wrong here?
here is my sql
SELECT
DATEADD(DD,
CONVERT(INT, (DATEDIFF(DD, '1/1/1900', t.DT)/7)) * 7, '1/1/1900') [WeekBeginDate],
SUM(HOURS) AS TOTAL_HOURS
FROM [DA2].[PMO].[TASK_TIME_TRACKER] t
WHERE UID = 'John07'
AND DT >= DATEADD(WEEK, -4, GetDate())
GROUP BY CONVERT(INT, DATEDIFF(DD, '1/1/1900', t.DT)/7)
DATEADD(DD, CONVERT(INT, (DATEDIFF(DD, '1/1/1900', CONVERT(DATE,t.DT))/7)) * 7, '1/1/1900')
You also need to change your group by expression to include the conversion. that will group each day together regardless of time, to display just the time you may need to wrap the whole line in an outer convert.
GROUP BY CONVERT(INT, DATEDIFF(DD, '1/1/1900', CONVERT(DATE,t.DT))/7)
What are you using to display the results? if it is SSRS you can change the format on the cell to date to drop the 0's that the timestamp would display.

Sybase- sql query where date in last x days without considering time part

I need to write a sybase query that will have a where clause with date within last x days like so -
SELECT *
FROM table_name
WHERE
date_col > [_last_x_days]
I was able to get datetime of last x days using
dateadd(day, -x, getdate())
However, the above method still gives me the time element based on when the query is run. How can I strip down the time part?
i.e. convert 10-10-2011 15:00:45 to 10-10-2011 00:00:00
Also, is there a better way to do this?
Thanks in advance!!!
J
The convert function will return the date without the time component.
dateadd( day, -x, CONVERT(DATE, getdate(), 103) )
See this link for a complete description of CONVERT.
How about this?
convert(datetime, substring(convert(varchar, dateadd(day, -x, getdate()), 20), 1, 11))