Select current date item by using ms sql server - sql-server-2008

I am stucking in sql statement .
Please my query below
select
master_brand.BRAND_NAME as AcCompanyID,
lewre_product.SKU_CODE as AcStockID,
lewre_product.SKU_CODE as BarCode,
lewre_product.SKU_CODE as StockBarCode,
lewre_product.SKU_CODE as StockDescription1,
NULL as StockDescription2,
lewre_project.PROJECT_NAME as AcStockBrandID,
lewre_artice.ARTICLE_NUMBER as AcStockGroupID,
icenter_acStockCategory.CATEGORY_CODE as AcStockCategoryID,
icenter_acStockColor.AcStockColorID as AcStockColorID,
lewre_product.SIZE as AcStockSizeID,
'NA' as ISBN,
master_product_type.UOM as AcStockUOMID,
lewre_article_price.FINAL_COST as StockCost,
lewre_article_price.PRICE as StockPrice1,
0 as StockPrice2,
0 as StockPrice3,
0 as StockPrice4,
0 as StockPrice5,
lewre_artice.CREATE_DATE
from [LEWRE.ARTICLE]as lewre_artice,
[iCENTER.ACSTOCKCATEGORY] as icenter_acStockCategory,
[iCENTER.ACSTOCKCOLOR] as icenter_acStockColor,
[LEWRE.PRODUCT] as lewre_product,
[MASTER.BRAND] as master_brand,
[LEWRE.PROJECT] as lewre_project,
[MASTER.PRODUCT_TYPE] as master_product_type,
[LEWRE.ARTICLE_PRICE] as lewre_article_price
where
(icenter_acStockCategory.CATEGORY_ID = lewre_artice.PRODUCT_CATEGORY_ID
AND icenter_acStockCategory.PRODUCT_TYPE_ID = lewre_artice.PRODUCT_TYPE_ID)
AND lewre_product.ARTICLE_ID = lewre_artice.ARTICLE_ID
and lewre_product.COLOR_ID = icenter_acStockColor.COLOR_ID
and master_brand.BRAND_ID = lewre_artice.BRAND_ID
and lewre_artice.PROJECT_ID = lewre_project.PROJECT_ID
and master_product_type.PRODUCT_TYPE_ID = lewre_artice.PRODUCT_TYPE_ID
and lewre_artice.ARTICLE_ID = lewre_article_price.ARTICLE_ID
and lewre_artice.CREATE_DATE
order by lewre_artice.CREATE_DATE
Here is the result that i get
I want to select all row which create_date is current date.for example (2012-04-30)' . How can i get the result? Please help me

Add this to your where clause.
lewre_artice.CREATE_DATE >= cast(getdate() as date) and
lewre_artice.CREATE_DATE < cast(dateadd(day, 1, getdate()) as date)
getdate() returns the current timestamp and cast(getdate() as date) removes the time part. dateadd(day, 1, ....) adds one day. The comparison returns the rows where CREATE_DATE is within the interval.

Related

mysql date range query, when the field is not sent anything for the 'between' statement

I have this query, which returns me 7 records
Now if I add this line 'AND date_format (ds.date_emission,'% Y-% m-% d ') BETWEEN' 'AND' '', nothing returns me, I want that when the date range is empty I need that I also returned the 7 records
Please refer answer below.
I have added two variable replace inStartDate variable with a range start date and inEndDate with range end date.
SELECT ds.id
FROM documentos_salida ds
WHERE ds.terceros_id = 329
AND ds.estado =0
AND ds.tipo_documento ='Factura'
AND ds.saldo_pendiente_factura >0
AND (ds.fecha_vencimiento > now() or
ds.fetcha_vencimiento = NOW())
AND ds.numero_documento LIKE '%%'
AND ( CASE WHEN (inStartDate = '' AND inEndDate = '')
THEN TRUE
ELSE (date_format(ds.fetcha_emision, '%Y-%m-%d') BETWEEN inStartDate AND inStartDate)
END)
ORDER BY ds.fecha_emision DESC;

how to count last 7 record of one table using mysql stored procedure ,in my query i want count isprinted as printed when isprinted=1

CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_printnotprintdailyexpenses`(
in i_localBodyId varchar(10),
in i_epId int(20),
out printed INT(20),
out notprinted INT(20)
)
BEGIN
set printed=(select count(isPrinted) from tbl_dailyExpenses
where date((curdate() - 7)) and date(curdate())and
localBodyId =i_localBodyId and epId=i_epId and isPrinted=1 group by CURDATE()-7 );
set notprinted=(select count(isPrinted) from tbl_dailyExpenses
where date((curdate() - 7 )) and date(curdate())and
localBodyId =i_localBodyId and epId=i_epId and isPrinted=0 group by CURDATE()-7 );
END
I don't think you need a procedure. A simple query should suffice. Based on what I have been able to understand from this question (and the other one you asked) this should give you the results you want:
SELECT date,
localBodyId,
epId,
SUM(CASE WHEN isPrinted=1 THEN 1 ELSE 0 END) AS printed,
SUM(CASE WHEN isPrinted=0 THEN 1 ELSE 0 END) AS notprinted
FROM tbl_dailyExpenses
WHERE localBodyId = i_localBodyId AND epId = i_epId AND
date >= NOW() - INTERVAL 7 DAY
GROUP BY date, localBodyId, epId
ORDER BY date DESC
LIMIT 7
In this query you will need to replace i_localBodyId and i_epId with the values you want to test.

Group by changing boolean value

Hi i have the following SQL question:
SELECT station_id, filling_station_status,date_created ,
case when filling_station_status="FREE" then 0
else 1 end as status
FROM efahrung.electric_station_time_status
where station_id=11
In my table have a column filling_station_status.
It can be "FREE" or "IN_USE".
I want to group elements so, that if the filling_station_status is changed (from "FREE" to "IN_USE") it will create a date range in my case, date_created.
In the next change again from ("IN_USE" to "FREE") it creates a new date range.
Thanks for a suggestions.
If you just need SQL query to generate date range in output, then try this:
Select s.station_id,
Coalesce(e.filling_station_status, s.filling_station_status) fillingStationStatus,
case e.filling_station_status
when "FREE" then 0 else 1 end status,
s.date_created startDate,
e.date_created endDate
From efahrung.electric_station_time_status s
Left Join efahrung.electric_station_time_status e
On e.station_id = s.station_id
and s.filling_station_status = 'IN_USE'
and e.filling_station_status = 'FREE'
and e.date_created =
(Select Min(date_created)
From efahrung.electric_station_time_status
Where station_id = s.station_id
and date_created > s.date_created)

How to write a Linq query equivalent to this SQL

I'm trying to figure out how to write a linq query that will return equivalent results to the sql query below. The problem I'm having has to do with the two select count queries included in the select list of the main query. I need to get counts of two different types of records from the PaymentHistory table for the last year. Can the equivalent of this be written using linq? Preferrably using lambda syntax.
select ieinum, serviceaddrkey,
(select count(*) from PaymentHistory where serviceaddrid = serviceaddrkey
and PostDate >= DateAdd(year, -1 , GetDate())
and SetID = 100) as ReturnedFees,
(select count(*) from PaymentHistory where serviceaddrid = serviceaddrkey
and PostDate >= DateAdd(year, -1 , GetDate())
and SetID = 101) as CorrectedReturnedFees
from Serviceaddr
Any help will be great.
Perhaps something like this:
from s in Serviceaddr
let ph = PaymentHistory.Where(p => s.serviceaddrkey == p.serviceaddrkey &&
p.PostDate >= DateTime.Now.AddYears(-1))
select new
{
s.ieinum,
s.serviceaddrkey,
ReturnedFees = ph.Count(p => p.SetID == 100),
CorrectedReturnedFees = ph.Count(p => p.SetID == 101)
}

Beginner LINQ syntax question

I have a basic SQL Table ( pKey INT, TransDate smallDateTime, Amount Float)
I simply want to emulate this SQL in LINQ
SELECT SUM(Amount) AS result
FROM dbo.Basic
WHERE TransDate >= #startDate
AND TransDate <= #EndDate
I have created the LINQ dbml for this and I can get basic query results for a date range
However I can't find the right syntax to get the SUM over a dateRange, I've tried several variations on the following but either they dont compile, or the result they give cannot be converted to a double
BasicDataContext dContext = new BasicDataContext();
var lnq = from c in dContext.Basic
where c.TransDate >= startdate &&
c.TransDate <= enddate
select new { total = c.Sum(Amount) };
double result = (double)lnq.total;
This should work:
double result = (from c in dContext.Basic
where c.TransDate >= startdate &&
c.TransDate <= enddate
select c.Amount).Sum();