I am trying to get transaction count month to date. For now I am providing date between from_date and to_date.
How can I get results for a particular month in month to date ?
You don't mention what SQL your using, but if its SQL Server then it has a built in function DatePart description I'd expect most of the major SQL dialects to have something similar.
So your where clause would be something like where datepart(mm, to_date) = 4 (for April)
If your version SQL doesn't have the date functions, then you can fallback to the cruder string manipulation. Assuming your date is yyyymmdd, then you could use a sub string function to pull out the mm part, convert that to a integer and perform your comparison.
Related
I have the table ORDERS and while trying to find the processing time for each order by subtracting the date of receipt from the date of delivery, I keep getting this error:
SQL0206N "DAY" is not valid in the context where it is used.
SQLSTATE=42703
My query is the following:
SELECT DATEDIFF(DAY, ReceiptDate, DeliveryDate) FROM ORDERS
I suspect it has something to do with the date's format in the columns ReceiptDate and DeliveryDate: DD/MM/YY. Does this have to be converted? What am I doing wrong?
EDIT: Is there an alternative for using DATEDIFF? Can I convert each individual date to an int of days and then subtract the two ints?
If you are using DB2, then there is no DATEDIFF function, which is specific to SQL Server. However, we can easily simulate it by taking a difference of days, using the DAYS() function:
SELECT DAYS(DeliveryDate) - DAYS(ReceiptDate) AS days_diff
FROM ORDERS;
I want week number from different columns of Year, Month & Day in same table of Hive QL.
IF you can give logic on SQL, that's also fine.
Concatenate the date, month and year into a proper date format and apply weekofyear().
Select weekofyear(cast(concat(year,"-",month,"-",date) as date)) from tablename.
Please note that I have used cast to convert the concatenated string into date.Howver, you might need to use different method based on your date format. Please refer to the below answer on handling string conversion to date formats.
Hive cast string to date dd-MM-yyyy
I am bit confusing here?
declare #date1 datetime = '2016-01-21 14:10:47.183'
I want to convert '2016-01-21 14:10:47.183' To '21-01-2016'
when I tried: select convert(date,#date1,105)
I am getting: 2016-01-21
But with: select convert(varchar(10),#date1,105)
I am getting: 21-01-2016
Why I am not having same results with above code?
Why should I convert to varchar?
Thanks in advance
This is just presentation matter and should be done in application layer. If you cannot do it in application you could use FORMAT (SQL Server 2012+):
declare #date1 datetime = '2016-01-21 14:10:47.183'
SELECT FORMAT(#date1, 'dd-mm-yyyy');
LiveDemo
Why I am not having same results with above code?
select convert(date,#date1,105)
-- DATETIME -> DATE
-- vs
select convert(varchar(10),#date1,105)
-- DATETIME -> VARCHAR(10) using specific style
If you only to skip time part use SELECT CAST(#date1 AS DATE) and do not bother how it is presented. It is still DATE.
To sum up: in SQL query use DATE as date, in application display it with desired format.
The reason why is because once you put a value in a datetime column (or date or any of the other variations on date-time datatypes) in SQL Server. SQL Server ceases to think of that date as having any particular format. It translates it into numbers, and stores it that way internally.
So when you select a date from a date time column, SQL Server displays it in the default format that you have selected based on your environment/local settings.
If you want to display it in any other format, you have to first convert it to a string, because as far as SQL Server is concerned, dates don't have formats. They are just numbers. The 21st day of March is the 21st day of March, whether you write it as 3/21 or 21/3.
So when you try to convert a date to a date with a different format, SQL Server just ignores you because dates don't have formats. However, if you want to convert that date to a string, SQL Server will be happy to help you display that string in any format you like.
Hope this helps, but sounds like some further research into how SQL Server stores dates would help your understanding.
I'm trying to make a query dynamic that looks for all the dates between two days of the year (July 1 of the last year and June 30th of this year). Below is the where clause of the query I've been attempting:
and pbh.batch_date between
('30-JUN-'||extract(year from trunc(sysdate))-1))
and ('01-JUL-'||extract(year from trunc(sysdate))))
This returns an inconsistent datatypes error: expected DATE got NUMBER. I then tried casting them to dates like so:
and pbh.batch_date between
to_date(( '01-JUL-'||extract(year from trunc(sysdate))-1))
and to_date(( '30-JUN-'||extract(year from trunc(sysdate))))
But this now returns an invalid number error. I don't know if it makes a difference what format pbh.batch_date is stored as, but it's (m)m/(d)d/yyyy
There's no need to cast to a string and back:
and ph.batch_date >= add_months(trunc(sysdate,'YEAR'), -6)
and ph.batch_date < add_months(trunc(sysdate,'YEAR'), 6)
I have datetime values in my database table like 05/05/2015 23:00:00. I am putting date filter in my query and try to fetch all the data of 05/05/2015 like this:
select *
from table
where date <= "05/05/2015".
It's not returning the records which have value 05/05/2015 23:00:00 in database.
Please suggest the way..
Using this where clause here
where date <= "05/05/2015"
means: return every row with a date before 05/05/2015 (including the ones with 05/05/2015 00:00:00 - but nothing more).
If you want to get all records for that day, too, you should use
where date < '20150506'
I'd also recommend to use the ISO-8601 date format yyyyMMdd to prevent any regional settings from interfering with your strings representing dates.
And I would also recommend to use something more expressive than just date for your column name - in SQL Server 2008 and newer, DATE is a reserved T-SQL keyword, too - use something like HireDate, SaleDate or something that tells you want kind of date this is