if the month is january date must be <= 31 then true or else false - sql-server-express

if the month is january then date must be <= 31 then validate true or false
select NO, if (month(colomn)=1)) (if Date(NO)<=31,'pass','fail,) ,'pass','fail') as result from table
select NO, if (month(colomn)=1)) (if Date(NO)<=31,'pass','fail,) ,'pass','fail') as result from table
2019/01/24 result true
2019/01/33 result false

You should not be working with text dates in your SQL Server database if you can avoid it. One simple tool available if you are using SQL Server 2012 or later is the TRY_CONVERT function. TRY_CONVERT will attempt to parse your date string (in its current format, which it can handle), and will either return a datetime or NULL, the latter in the case when the date is bogus. For example:
SELECT
TRY_CONVERT(datetime, '2019/01/24') AS dt1, -- 24/01/2019 00:00:00
TRY_CONVERT(datetime, '2019/01/33') AS dt2; -- NULL, can't be parsed
The value for dt2 is NULL, since there is no 33rd day of January.

Related

What is the difference TO_DATE('21-09-1989','DD-MM-YY') and TO_DATE('21-09-89','DD-MM-YY')?

Below query returns the result:
SELECT *
FROM EMPLOYEES
WHERE HIRE_DATE = TO_DATE('21-09-1989','DD-MM-YY');
where as if I change the date from 21-09-1989 to 21-09-89 returns nothing.
SELECT *
FROM EMPLOYEES
WHERE HIRE_DATE = TO_DATE('21-09-89','DD-MM-YY');
What is the issue here?
If you use the YY as the year then it returns the year which is in the current century i.e. 2000-2099. In your case -- 2089
If you use the YYYY then the exact year is returned. -- in your case 1989
If you use RR then the year which is between 1950-2049 is returned. -- in your case 1989
So
TO_DATE('21-09-1989','DD-MM-YY') --> 21-09-1989
-- oracle is smart to detect the given format as YYYY
TO_DATE('21-09-89','DD-MM-YY') --> 21-09-2089
-- as 2089 is between 2000-2099
TO_DATE('21-09-89','DD-MM-RR') --> 21-09-1989
-- as 1989 is between 1950-2049
TO_DATE('21-09-89','DD-MM-YY')
evaluates to Sep 21 2089
As already explained in other answer about the difference between 'YY' and 'YYYY' format and its implications. You could easily avoid problems by using the ANSI DATE literal:
DATE '1989-09-21'
The ANSI date literal contains no time portion, and must be specified in exactly this format ('YYYY-MM-DD').
Also, HIRE_DATE is a DATE data type which also has a time portion. Therefore, your WHERE condition might not return correct values:
WHERE HIRE_DATE = TO_DATE('21-09-1989','DD-MM-YY')
To remove the time portion you must use TRUNC to compare with only date portion:
WHERE TRUNC(HIRE_DATE) = DATE '1989-09-21'

SQL Server end of month

Suppose there is one date in int format 20191229, I want to find end of month and check if it's end of month is of 31 days or 30 days in SQL Server
You can try this from the reference. The given answer will not work for the integer data type but it will work in the varchar datatype date value. Storing Date values in integer is not a good idea, so as suggested by Larnu change the data type in either date or varchar.
SELECT
Day(EOMONTH(Cast('20191229' as Date))) end_of_month;
If you want the amount of days within a month, as you need the days as an integer, you should go for this. This is the most robust built, but also the more complex one, as to make sure the Integer value is processed correctly:
SELECT DATEPART(DAY,EOMONTH(CAST(CAST('20191229' AS NCHAR(8)) AS DATE))) AS Days
Result:
Days
31
If you want to add an IF evaluation to your selected date(s), you can do this by add an IIF-clause where it evaluates whether or not the end of month is 31 or not. If you want to use a columnname instead of the date, just substitute #Date with the columnname. I've just added the variable #Date instead of '20191229' to make it more illustrative/understandable. You can change the True/false descriptions to whatever you like in the query.
DECLARE #Date AS int
SET #Date = '20191229'
SELECT
IIF (
DATEPART(DAY,EOMONTH(CAST(CAST(#Date AS NCHAR(8)) AS DATE))) = '31'
,'True'
,'False'
) AS Days
Output:
Days
True

SQl Server Converting to Date fails , DateTime works

I have a table with a varchar(25) column that holds a date value. A typical value is '11/04/2017'.
This query returns 0 rows
select *
from myTable
where isdate(inputDate) = 0
I am trying to find a max on this, using a date sort.
This query returns the expected result
;with gooddates as
(
select
medcomfolder, PatientId, PatientBirthday, InputDate
from
myTable
where
isdate(inputDate) = 1
)
select max(convert(datetime, inputDate))
from gooddates
This query returns an error.
;with gooddates as
(
select
medcomfolder, PatientId, PatientBirthday, InputDate
from
dwhFuData
where
isdate(inputdate) = 1
)
select max(convert(date, inputdate))
from gooddates
This is the returned error
Msg 241, Level 16, State 1, Line 274
Conversion failed when converting date and/or time from character string
The difference between the 2 queries is that the first is converting to a dateTime while the latter is converting to a date.
At this point, I can move forward w/ the dateTime option, but I am left wondering what I am missing.
I have checked that there are no embedded spaces, and all the columns have a len(InputDate) = 10 (there is NO time data included)
I selected distinct values,put them in excel, and did a date function on each row. I was hoping to get a #VALUE on 1 row. All the rows worked.
So there is nothing silly like '02/31/2019' going on.
How can a dateTime conversion pass when a simple date conversion does not?
My guess is that you have values that include a time stamp following the date (based on the fact that isdate() is always zero).
If so, one simple solution would be to use convert(date, left(inputdate, 10)). Another solution uses try_convert():
try_convert(date, inputdate)
To find the offending values:
select inputdate
from dwhFuData
where try_convert(date, inputdate) is null and inputdate is not null;

How to check if column having date of birth format has yyyymmdd in sql server?

How to check if column having date of birth format has yyyymmdd in sql server?
You can verify with the ISDATE Function, but I don't know which SQL edition do you have
Example
select ISDATE ( 1 )
------------------
0
select ISDATE ( 11111111 )
------------------
0
select ISDATE ( 20170501)
------------------
1
You can use:
select (case when try_convert(date, dob) is not null and
try_convert(int, dob) is not null
then 1 else 0
end)
I'm not 100% sure, but I think that yyyymmdd is the only format that will generally pass both conditions. Note: There is no way to know if 20170601 is really June 1st or Jan 6th, so this cannot actually validate the contents of the field.
But why do you care what the format is, so long as you can convert it to a date? You should then change the column to a date type and henceforth know that the "format" is correct.
If the column is a Char type (yuck) then Like '[12][0-9][0-9][0-9][0-1][0-9][0-3][0-9]' (off the top of my head)
If it's a DateTime then who cares, it's a date.

How do I convert date like '31/06/2013' or '30/02/2013' of datatype varchar in SQL Server 2008

I have a column named next_due_date datatype varchar and in this column some non-existing dates like 31/06/2012 or 30/02/2013 are saved. Because of this I get an error message when I convert it to date datatype.
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
If you dates would be valid (not something like the 31st of June or 30th of February - those dates simply don't exist!), then you could easily use the CONVERT function to convert them to DATE:
DECLARE #DateTable TABLE (DateColumn VARCHAR(20))
-- **VALID** dates - 30th of June, 28th of Feb
INSERT INTO #DateTable(DateColumn) VALUES ('30/06/2012'), ('28/02/2013')
-- easily converted to DATE type using style #104
SELECT DateColumn, CONVERT(DATE, DateColumn, 104)
FROM #DateTable
You can use try_parse (which is only available on higher SQL version, from 2012 on wards). It checks if a date is valid. If so, it returns the date, else null.
I think en-GB is the culture you need:
select try_parse('30/06/2012' as date using 'en-GB') -- returns a valid date
select try_parse('31/06/2012' as date using 'en-GB') -- returns null
If you need this on lower platforms, I would suggest to parse it by hand using a procedure. Something like this:
BEGIN TRY
select CONVERT(date,'31/06/2012',103)
END TRY
BEGIN CATCH
select null
END CATCH