in sql server 2012 i cant not give data type for days like sunday monday , and my project need compare the days between sql and System which contains days like sunday, monday... , please help me how can i proceed ahead?
Try the following query:
SELECT CASE
WHEN CAST(dateX AS DATE) != CAST(dateY AS DATE)
then 'NOT EQUEL'
else 'EQUEL'
Related
I need to convert the following SQL server code to PostgreSQL code.
Any help would be appreciated.
SQL Server SQL:
CAST(DATEADD(ww,DATEDIFF(ww,0,trans_date),-1)as date) as week
I think what that code does is to "round" the value of trans_date to the beginning of the week. In Postgres you can do that using the date_trunc() function:
date_trunc('week', trans_date)
Note that this always returns a timestamp, if you need a real date value, cast the result:
date_trunc('week', trans_date)::date
If it should be the day before the beginning of the week, just subtract one day from the result:
date_trunc('week', trans_date)::date - 1
I'm trying to right an SQL Query that will do the following task:
Example:
ExpirationDate = '2018-04-30 00:00:00.000'
when the property is set to expire in 30 days ... 1 month before, I want SQL Server to email me informing that the item is about to expire based upon (Expirationdate)
could someone please give me some pointers as to where I go next... this is what I have tried so far..
the query below will find the item however I now want SQL Server Agent to send me an email for each row that is returned, not when nothing is returned
SELECT *
FROM [dbo].[Property]
WHERE expirationdate <= DateAdd(day ,30 , GetDate())
Here is an excellent link on how to email results from a sql server job agent:
https://www.brentozar.com/archive/2014/10/send-query-results-sql-server-agent-job/
As for your query: I wouldn't SELECT *. Explicitly select the columns you want returned in your email response.
Next I would define a variable to hold the value of your cutoff, this will prevent you doing a calculation for each row within the query. Example:
DECLARE #day30UpperBound DATETIME = DATEADD(Day, 31, cast(getdate() as date));
DECLARE #day30LowerBound DATETIME = DATEADD(Day, 30, cast(getdate() as date));
Then you can query results with:
SELECT explicitColumnList FROM [dbo].[Property]
WHERE expirationdate BETWEEN #day30LowerBound AND #day30UpperBound
This specific example should give you those Properties with an expiration date that fall in the window of 30 days from current at midnight, to 31 days from current at midnight - so a full 24 hour window 30 days from current day.
You can easily reproduce this for your 15 day, and 7 day reports. You could put all 3 reports into one job agent. Or you could make a different job for each of the 3.
You'll have to research a little on how to do only business days if that's what you want.
So there is a very similar forum post to this one but I cannot run those commands. I am using SQL Server 2008 R2 and need help getting the current school year.
Basically I need a way to generate the current school year by using the current date. From Aug. of the current year until Aug. of the next year it will be 1 school year.
Ex. Aug. 2015 - July 2016 will need to pull up the year 2015 and Starting Aug. 2016 the year 2016 will need to pull up.
I am still fairly new to SQL so any help would be greatly appreciated.
Sounds like you're looking for this:
IF Month(GetDate()) >=8
BEGIN
SELECT Year(GetDate())
END
ELSE
BEGIN
SELECT Year(GetDate())-1
END
EDIT:
To use this in a select statement's where clause:
DECLARE #currentSchoolYear INT
IF Month(GetDate()) >=8
BEGIN
SELECT #currentSchoolYear = Year(GetDate())
END
ELSE
BEGIN
SELECT #currentSchoolYear = Year(GetDate())-1
END
SELECT * FROM dbo.dates
WHERE CASE WHEN Month(datevalue) >=8 THEN Year(datevalue)
ELSE Year(datevalue)-1
END = #currentSchoolYear
obviously replace "dbo.dates" with your table name, and "datevalue" with whatevercolumn you're comparing. This will return the rows from the current school year
SELECT value1, value,
(CASE WHEN GETDATE() BETWEEN '20150801' AND '20160731' THEN 2015
WHEN GETDATE() BETWEEN '20160801' AND '20170731' THEN 2016
ELSE 2014) AS schoolYear
FROM dbTable
WHERE ...
I'm pulling the data from SQL database. I have a couple columns with date which need to be converted into Int type, but when I do this the date changes (-2 days). I tried Cast and Convert and it's always the same.
Converting to other type works fine and returns the correct date, but doesn't work for me. I need only the date part from datetime and it needs to be recognised as a date by Excel.
Why is this happening? Any ideas how to get it sorted?
I'm using the following query:
SELECT wotype3, CONVERT(INT,wo_date2 ,103), CAST(duedate AS int) FROM Tasks WHERE
duedate > DATEADD(DAY,1, GETDATE())
AND wo_date2>0
AND wo_date2<DATEADD(WEEK,3,GETDATE())
ORDER BY wotype3
I've had big problems with this, checking my SQL Server's calculation results with "expected results" which a user had created using Excel.
We had discrepancies just because of this 2-day date difference.
Why does it happen ?
Two reasons:
SQL Server uses a zero-based date count from Jan 1 1900, but Excel uses a 1-based date count from Jan 1 1900.
Excel has a bug in it (gasp!) which makes it think that the year 1900 was a leap year. It wasn't. SQL Server correctly refuses to let you have a date value containing "29-Feb-1900".
Combine these two discrepancies, and this is why all dates, from March 1 1900 onwards, are always 2-days out.
Apparently, this Excel bug is a known issue, to keep it in line with Lotus 1-2-3.
The Intentional Date Bug
Microsoft's own explanation
From now on, I think I'll justify bugs in my code with the same excuse.
;-)
For SQL Server 2008 and above, you can use the DATE datatype.
declare #dt datetime = '12/24/2013 10:45 PM' -- some date for example
SELECT #dt as OriginalDateTime, CAST(#dt as DATE) as OnlyDate
For versions prior to SQL Server 2008, you would need to truncate the time part using one or the other functions. Here is one way to do that:
declare #dt datetime = '12/24/2013 10:45 PM' -- some date for example
SELECT #dt as OriginalDateTime, CAST(FLOOR(CAST(#dt AS FLOAT)) as DATETIME) as OnlyDate
I have the following MSSQL query to return the ending day of the week in MSSQL:
SELECT DateAdd(Day, 0 - DatePart(Weekday, GetDate()), GetDate());
I played around with the =DateAdd function, but it keeps throwing me an error for the Day parameter. Also, when I used DateInterval.Day... I get the same error.
However, when I try placing that query into an SSRS expression, it throws me an error. Does anyone know the direct conversion for that query above in SSRS?
SSRS Uses a dialect of Visual Basic, its Date functions are different from TSQL, you have to use
"d" instead of DAY for day interval
"w" instead of WEEKDAY for weekday
Now() instead of GetDate() for current date.
Try
=DateAdd("d", 0 - DatePart("w", Now()), Now())