In MS SQL I am trying to get the value of day in the current date, eg: today is 25/08/2016, so this value would be the number 25. I would also like to find the value of day in an earlier date (eg: 06/06/2016 so this number would be 06). I am then looking to subtract the second value from the first and determin if the result is a positive or negative value. If it is positive it should do one thing, eg print test A and if it is a negative value it should do something else, eg print test B.
I am new to MS sql and really have no clude how to implement this in the language. Does anyone have any pointers? Much appreciated. Please see my pseudo code:
change = value of days in current date - value of days in previous date
if change is a positive value : print "testA"
if change is 0 or a negaitve value: print "testB"
I am doing this in excel 2010. I have three columns with dates in colA & colB and testA or test B should be printed in colC depending if the value is positive or negative.
Eg data:
colA: 12/02/2016, 06/06/2016, 12/02/2016
colB: 12/05/2016, 12/02/2015, 28/06/2016
If you can able to change your dates format to MM/DD/YYYY then below script may help you
DECLARE #D1 DATETIME ='08/25/2016'
DECLARE #D2 DATETIME ='06/06/2016'
IF DATEPART(D,#D1) - DATEPART(D,#D2) > 0
PRINT 'testA'
ELSE
PRINT 'testB'
Use the below script :
SELECT CASE WHEN (DATEPART(dw,GETDATE())+DATEPART(dw,GETDATE()-1)) > 0 THEN 'testA'
WHEN (DATEPART(dw,GETDATE())+DATEPART(dw,GETDATE()-1)) =0 THEN 'testB' END Result
some additional info.
IF you need a particular DAY/Day Value use the below scripts
SELECT DATENAME(dw,'12/22/2016') --Thursday
SELECT DATEPART(dw,'12/22/2016') --5
IF you need a DAY of month value use the below script
SELECT DATEPART(dd,'12/22/2016') --22
Related
I have a data having 0,1,2 values in the row field and dates in increasing order in the column field and I would like the last '2' value keep constant moving forward in the dates. Please do let know a way to workaround. Example may 2027 has 2 and then 0 but I would like to have 2 in june 2027 and the rest of the dates.I would like to keep previousor beginning date values same but has the maximum value 2 in this case carry forward to the later dates.
Thank you.
Question
MS SQL Syntax:
SELECT TOP 1 value FROM YourTableName WHERE value <> 0 ORDER BY date_action DESC
MySQL Syntax:
SELECT value FROM YourTableName WHERE value <> 0 ORDER BY date_action DESC LIMIT 1
I have a month column with values from 1,2,3 up to 12. I am writing below query to convert column values with 1 digit to 2 digits that is values like 1 and 2 will be converted to 01 and 02, but that concatenation is not working, the month still remains as single digit.
Main query:
select
case
when len(month) = 1
then concat(0, month)
else month
end as month_new,
month
from
Table
But when I tried the query separately as below the concatenation works and it converts single digit month to 2 digits
Query 1
select top 10 concat(0, month), month
from table
Query 1 alone is working
Query 2
select
case
when len(month) = 1
then 1
else 0
end,
month
from
Table
Query 2 alone is working, means the checking of length in column month is working as expected. But when concat used inside case it is not working.
I have modified the query as below and worked for me
select
case
when len(month) = 1
then concat(0, month)
else cast(month as varchar)
end as month_new,
month
from
table
The problem is that month is an integer, whereas the result from concat() is a string. So. case is trying to cast the string back into an integer. You could force the integer into a string by using cast, but there are better ways to do this.
Instead, just use the FORMAT function:
select
format(month, '00') as month_new
, month
from viivscaazure.F_SALES_DETAIL
Don't know what database are you using and since you don't provide any sample data I only can assume that your CASE is not the problem, but if you want to do so that means your datatype is string and you tried to CONCAT string with integer in your query.
Maybe you can try to add "quote" to your zero string and CAST the result as a string.
I have a column due date in the format 20210701 (YYYYMMDD), using SQL I want to extract all the dates apart from 5th of particular month ( As highlighted in the pic below )
I used the below code:
SELECT Due_Date_Key
FROM Table
WHERE Due_Date_Key <>20210705
However the error in the above code is it will exclude only the month of jul but not for other months.
How can extract the dates apart from 5th from the entire column.
Help would be much appreciated.
Note that column DUE_DATE_KEY is numeric.
A more SQLish way would be to convert string to date and then check if day is not 5
SELECT * FROM Table
WHERE DATE_PART('day', to_date(cast(DUE_DATE_KEY as varchar), 'YYYYMMDD')) != 5
Using modulo operator to determine whether the last two digits of DUE_DATE_KEY are 05.
select * from T where DUE_DATE_KEY % 100 <> 5
Using your sample data, the above query returns the following:
due_date_key
20210701
20210708
20210903
Refer to this db fiddle
Regarding DateDiff function
select datediff(current_date, '-2018-01-21');
what is - here as I know datediff(enddata,startdate)
if we mention minus for startdate it getting number values 1474138
can please help to understand
Below query confirms that a negative date is similar to a negative integer. If you subtract a negative number to a positive number, it is the same as adding their absolute values (ignoring the signs). For example; 8 - (-4) = 8 + 4
Thus, since the minimum date value for date type is '0000-01-01', we measure the number of days from year -2018 to 0000 and add to the number of days from 0000 to 2018. Then, we get 1474137 ( = 737122 + 737015). Hope this helps. Thanks.
Query:
select datediff('2018-03-02', '0000-01-01'), datediff('0000-01-01', '-2018-03-01'), datediff('2018-03-02', '-2018-03-01')
Result:
737122 737015 1474137
Again, 737122 + 737015 = 1474137. There are 1474137 days since 2018-Mar-01 BC.
I am new to visual foxpro. I am trying to write the sql statements.
There are two columns of dates, data type is in numeric.
Column A date is in the YYYYMMDD format.
Column B date is in the YYYYMM format. DD is not available, thus I am only comparing the YYYYMM.
I need to subtract or find the difference between a specific date e.g. 31 August 2015 and the dates in column A and B. Once I have the difference, I need to compare and see if the difference in Column B is greater than Column A.
What I have thought is using substr and split the dates to YYYY and MM. Then I subtract it from the specific date, and then compare the YYYY portion to see if it column B is greater than column A.
Your description sounds as if columnA / 100 would give a comparable format.
So if you've got test data like these
CREATE CURSOR test (columnA Num(8), columnB Num(6))
INSERT INTO test VALUES (20150802, 201508)
INSERT INTO test VALUES (20150712, 201506)
... you can get all rows where colmumnB equals converted(columnA):
SELECT * FROM test WHERE INT(columnA / 100) = columnB
... or get the difference between A and B for all rows:
SELECT INT(columnA/100) - columnB FROM test
Or if you've got a date-type parameter, you can for example get all rows where columnB would match the parameter:
d = DATE(2015,8,31)
SELECT * FROM test WHERE columnB = YEAR(d) * 100 + MONTH(d)
If you want to do something different, I'd suggest to edit the question and add more details