how to get before date Rates using sqlserver? - sql

select rateperkg from K_FS_FeedMrpDetails where date = getdate()-1
I want to display rateperkg column before date values

Try this
get Yesterday
select rateperkg from K_FS_FeedMrpDetails where date = DATEADD(d,-1,GETDATE())
see this link for more details.
I think this will helps you
http://blog.sqlauthority.com/2008/08/29/sql-server-few-useful-datetime-functions-to-find-specific-dates/

This code will set time to zero.
declare #DaVal as datetime
--111 will return yyyy/MM/dd
--101 will return MM//dd/yyyy
set #DaVal = convert(datetime, convert(varchar(25), DATEADD(d,-1,GETDATE()), 111))
select rateperkg from K_FS_FeedMrpDetails where date = #DaVal

Try using DATEADD() function and cast it to Date to remove time part like below
SELECT rateperkg
FROM k_fs_feedmrpdetails
WHERE date = Cast(Dateadd(day, -1, Getdate()) AS DATE);

If your date column stores date and time then use the below condition
where date >=convert(date,getdate()-1)
and date < getdate()
else
where date = convert(date,getdate()-1)

Related

how to join date column and time column

i have a date column and a time column like this:
Date Column "11.01.2018 00:00:00"
Time Column: "01.01.1899 21:50:00"
I want to join these Date and Time to have a Result like this:
"11.01.2018 21:50:00"
Can you please help me?
select concat(CONVERT(date,getdate()),' ',CONVERT(VARCHAR(8),GETDATE(),108))
SELECT CAST(CONVERT(date, #datetime1) AS DATETIME) + CAST(CONVERT(char(10),
#datetime2, 108) AS DATETIME)
The below converts Date and Time columns to Datetime:
DECLARE #MyDate DATE = '2018-01-11'
Declare #MyTime TIME = '1899-01-01 21:50:00'
SELECT CAST(#MyDate AS DATETIME) + CAST(#MyTime AS DATETIME);

Select today's date as default

I have a question which I'm not sure if possible or not but let's say I have a table with Employeeid, Date and AttendanceStatus i.e "Present"/"Absent".
I can get the status of all employees for all dates or for dates I specify in the query.
My question is that is it possible to get today's date by default so whenever I run the query it give me the data rows for today's instead of all the records from the database. I want to use default so that I don't have to change the date everyday.
Thanks in advance
Try this:
select * from TABLE_NAME where
where cast([Date] as date) = cast(getdate() as date)
Also, as already mentioned, you could create a view:
create view V_TABLE_NAME as
select * from TABLE_NAME where
where cast([Date] as date) = cast(getdate() as date)
You can apply where clause for current day;
select * from table where
Date > cast(getdate() as date) and
Date < DATEADD(day,1,cast(getdate() as date))
Or you can create a view;
create view v_table
as
select * from table where
Date > cast(getdate() as date) and
Date < dateadd(day,1,cast(getdate() as date))
Then query;
select * from v_table
Try This Logic
DECLARE #MyDate DATE
SELECT
*
FROM YourTable
WHERE DateField = ISNULL(#MyDate,GETDATE())
Here, If You are not passing any values in the Parameter #MyDate, it will take Current Date as Default. The following can also be used
DECLARE #MyDate DATE
SELECT
*
FROM YourTable
WHERE
DateField = CASE WHEN ISDATE(#MyDate) = 1 THEN #MyDate ELSE GETDATE() END

How to get dates between time range in sql server

I need to write a stored procedure in which i will get begin date and end date. for eg: start date is 05/07/2015 and end date is 28/08/2015. I need to write a query which will fetch details between 01/07/2015 and 31/08/2015. It should fetch details between 1st day of month selected for begin date and last day of month selected for end date.
This is what I tried, but it isn't working:
DATEDIFF(month,'2014-06-05','2014-08-05')
You can compare the dates using the common operators "<" and ">",
Example:
select * from table_name where '2014-06-05' < datefield and datefield < '2014-08-05'
DECLARE #StartDate DATETIME
DECLARE #EndDate DATETIME
SET #StartDate = convert(DATE, '20140705' ,112)
SET #EndDate = convert(DATE, '20140828' ,112)
SELECT * FROM [TableName] WHERE [TableName].DateField BETWEEN
dateadd(d,-(datepart(d, #StartDate)-1),#StartDate) AND
dateadd(d,-1,dateadd(m,1, dateadd(d,-(datepart(d, #EndDate)-1),#EndDate)))
It sounds like you need to find the first day of the month for '05/07/2015' and the last day of the month for '28/08/2015', then use those as the limits for your where clause.
This will easily convert a date to the first of the month:
declare #firstDate datetime = '2015-07-05'
declare #firstOfMonth datetime = dateadd(month,datediff(month,0,#firstDate),0)
select #firstDate
-- returns '2015-07-01 00:00:00.000'
To get to the last day of a month, use this:
declare #lastDate datetime = '2015-08-28'
declare #lastOfMonth datetime = dateadd(second,-1,dateadd(month,datediff(month,0,#lastDate)+1,0))
select #lastOfMonth
-- returns '2015-08-31 23:59:59.000'

SQL Output to get only last 7 days output while using convert on date

I am using the below SQL Query to get the data from a table for the last 7 days.
SELECT *
FROM emp
WHERE date >= (SELECT CONVERT (VARCHAR(10), Getdate() - 6, 101))
AND date <= (SELECT CONVERT (VARCHAR(10), Getdate(), 101))
ORDER BY date
The data in the table is also holding the last year data.
Problem is I am getting the output with Date column as
10/11/2013
10/12/2012
10/12/2013
10/13/2012
10/13/2013
10/14/2012
10/14/2013
10/15/2012
10/15/2013
10/16/2012
10/16/2013
10/17/2012
10/17/2013
I don't want the output of 2012 year. Please suggest on how to change the query to get the data for the last 7 days of this year.
Instead of converting a date to a varchar and comparing a varchar against a varchar. Convert the varchar to a datetime and then compare that way.
SELECT
*
FROM
emp
WHERE
convert(datetime, date, 101) BETWEEN (Getdate() - 6) AND Getdate()
ORDER BY
date
Why convert to varchar when processing dates? Try this instead:
DECLARE #Now DATETIME = GETDATE();
DECLARE #7DaysAgo DATETIME = DATEADD(day,-7,#Now);
SELECT *
FROM emp
WHERE date BETWEEN #7DaysAgo AND #Now
ORDER BY date
Use this, simply.
Select columnname
from tablename
WHERE datecolumn> dateadd(day,-7,GETDATE())

How can I obtain only MSSQL rows that have been created since Jan 1st?

I have a script that writes database information to a csv based on a SQL query I wrote. I was recently tasked with modifying the query to return only rows where the DateTime field has a date that is newer then Jan. 1 of this year. The following query does not work:
$startdate = "01/01/2013 00:00:00"
SELECT Ticket, Description, DateTime
FROM [table]
WHERE ((Select CONVERT (VARCHAR(10), DateTime,105) as [DD-MM-YYYY])>=(Select CONVERT (VARCHAR(10), $startdate,105) as [DD-MM-YYYY]))"
The format of the DateTime field in the database is in the same format as the $startdate variable. What am I doing wrong? Is my query incorrectly formated? Thanks.
DECLARE #startdate datetime
SELECT #startdate = '01/01/2013'
SELECT Ticket, Description, DateTime
FROM [table]
WHERE DateTime >= #startdate
you can get year based records through DATEDIFF
DECLARE #startdate datetime = '01/01/2013';
SELECT Ticket, Description, DateTime
FROM [table]
WHERE DATEDIFF(YEAR,#startdate,DateTime) = 0 //It gives 0 if years are the same and DateTime is your column
You can get the start of the current year using:
DateAdd( year, Year( GetDate() ) - 1900, 0 )
The query then becomes:
select Ticket, Description, [DateTime]
from [Table]
where [DateTime] >= DateAdd( year, Year( GetDate() ) - 1900, 0 )
If you really don't want rows from January first then change the comparison from >= to >.
DECLARE #startdate datetime
SELECT #startdate = '01/01/2013'
SELECT Ticket, Description, DateTime
FROM [table]
WHERE DateTime >= #startdate
Interesting question that says as much about interpreting the requirement as about the SQL. When next year comes you will want to select next year's tickets. So it should be
SELECT Ticket, Description, DateTime
FROM [table]
WHERE datePart(yy,DateTime)=datePart(yy,getDate())
DECLARE #startdate datetime = '01/01/2013';
SELECT Ticket, Description, DateTime
FROM [table]
WHERE DATEDIFF(DD,#startdate,DateTime) >= 0