This question already has answers here:
What is the significance of 1/1/1753 in SQL Server?
(6 answers)
Closed 6 years ago.
The MS doc states that ISDATE()
Returns 1 if the expression is a valid date, time, or datetime value; otherwise, 0
So why is it returning 0 in the example below?
DECLARE #DT VARCHAR(30) = '1/4/1752'
SELECT
ISDATE(#DT),
TRY_CONVERT(DATE, #DT, 101),
TRY_CONVERT(DATETIME, #DT),
TRY_CAST(#DT as DATE),
TRY_CAST(#DT AS DATETIME)
returns
0 1752-01-04 NULL 1752-01-04 NULL
Change the date to 1753 and ...
1 1753-01-04 1753-01-04 00:00:00.000 1753-01-04 1753-01-04 00:00:00.000
select ISDATE('17521231'), ISDATE('17530101') gives
0 1
As explained in the documentation, the earliest datetime value is '1753-01-01'.
I would suggest that you use try_convert() instead. This gives you more flexibility:
try_convert(date, '17521231') is not null
The date data type goes back to year one.
Related
This question already has answers here:
SQL Server 2005 DateTime (return only hh:mm)
(2 answers)
Closed 6 years ago.
Done automatically add the date to the time of the database SQL Server 2005 Express. So when you add a new record in the database is added automatically date and time in the form of:
Create Table Baza_test
(
ID bigint IDENTITY (1,1) NOT NULL,
Client nvarchar (23)
address nvarchar (46)
DateOfAddmission datetime default CURRENT_TIMESTAMP
aaaaaadres nvarchar (46)
);
How to get alone time (highlighted in red):
with SQL Server database?
With the following query:
select CONVERT(VARCHAR(5), DateOfAddmission,108) AS [time] from Baza_test where ID = 1;
There is already similar question out there, please refer to the link bellow:
https://stackoverflow.com/a/7710495/4558361 by t-clausen.dk
The CONVERT(VARCHAR) answers are valid, if you want more flexibility and don't know the conversion codes like me :), you could do it with FORMAT:
DECLARE #d DATETIME = GETDATE();
SELECT FORMAT( #d, 'hh:mm') AS 'Time Result'
If 2012+
Select format(GetDate(),'HH:mm') --For the 24 clock
Select format(GetDate(),'hh:mm tt') --For the 12 clock
That said, I was informed that FORMAT() has a poor performance
Try like this,
12 HOURS FORMAT
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), DateOfAddmission, 100), 7)) FROM Baza_test
24 HOURS FORMAT
SELECT CONVERT(VARCHAR(5),DateOfAddmission, 108) FROM Baza_test
Apart from the answer given here another way is this -
SELECT CAST(CAST(GETDATE() AS Time(0)) AS VARCHAR(5))
Output
05:48
And in your case its -
SELECT CAST(CAST(DateOfAddmission AS Time(0)) AS VARCHAR(5)) AS [time]
FROM Baza_test
WHERE ID = 1;
This question already has answers here:
How to return only the Date from a SQL Server DateTime datatype
(46 answers)
Closed 6 years ago.
How can I display only date while using getdate() function?
When I am using
select getdate() as Today's Date
It is giving me:
2016-05-18 08:32:07.100
But I only need:
2016-05-18
How to get this?
Use convert. The first argument truncates the length, and the last specifies the format you want the date in:
SELECT Convert(varchar(10), getdate(), 103) as [Today's Date]
If you are using SQL Server 2012+, you may also want to look at FORMAT - which in my opinion is a bit nicer to read/use.
Example: SELECT FORMAT(GETDATE(), 'dd/MM/yyyy')
See this msdn link for more info
When I am executing following query I am getting different results.
SELECT Datediff(year, 0, Getdate());
The result was 115
When I use this, I am getting another result:
SELECT Datediff(year, 1900, Getdate());
The result was 110
Actually in SQL Server it will take from 1900-01-01, but why do these show different values?
Try this to explain the logic:
select cast(0 as datetime)
select cast(1 as datetime)
An integer is interpreted as the number of Days since 1900-01-01 whereas a string value such as '1900' will be interpreted as a date format.
1900 Days from Jan 1st 1900 is 1905-03-16, which is five years from 1900 and 110 years from now (2015).
This is because if you cast 0 as datetime, it returns 1900 as the year part, whereas 1900 cast as datetime returns 1905 as the year part.
Demo
From MSDN:
Values with the datetime data type are stored internally by Microsoft SQL Server as two 4-byte integers. The first 4 bytes store the number of days before or after the base date, January 1, 1900. The base date is the system reference date.
That means, casting the literal 0 to datetime is equivalent to getting the datetime value for 0 days after 1/1/1900, which is 1/1/1900. Similarly for 1900. Therefore, as #MartinSmith points out in the comments, your calculation is equivalent to SELECT Datediff(year,dateadd(d,0,'1/1/1900'), Getdate()) which returns 115 as expected.
Possibly worth noting that the MSDN page on Cast and Convert does not specifically cover this scenario i.e. int to datetime.
The number you specified will be added as days which resulted in the difference.
Select DATEADD(dd,0,0)
Select DATEADD(dd,1900,0)
Result1 is 1900
Result2 is 1905.
So using them is equal to:
SELECT Datediff(year,0, Getdate()) = SELECT Datediff(year,DATEADD(dd,0,0), Getdate());
SELECT Datediff(year,1900, Getdate()) = SELECT Datediff(year,DATEADD(dd,1900,0), Getdate());;
This question already has answers here:
Compare time part of DateTime data type in SQL Server 2005
(6 answers)
Closed 4 years ago.
I have a table for store takt-time. It's compound with "day_shift(1)" and "night_shift(0)" like below.
If I put current time(by using GETDATE() function ).
I need to know this current time is "day(1)" or "night(0)". my query like this.
SELECT [day_shift_flag]
FROM [company_working_time]
WHERE GETDATE() BETWEEN [start_time] AND[end_time]
But it can't found because a date received from GETDATE() isn't '1900-01-01'
How to compare a only time and ignore day,month,year.
What should I do?
You can cast a DateTime as a Time data type. For example:
SELECT
[day_shift_flag]
FROM [company_working_time]
WHERE
CAST(GETDATE() AS TIME) BETWEEN CAST([start_time] AS TIME) AND CAST([end_time] AS TIME)
By Following Way You can Ignore the day,month,year
SELECT [day_shift_flag]
FROM [company_working_time]
WHERE DatePart(HH,GETDATE()) BETWEEN DatePart(HH,[start_time]) AND DatePart(HH,[end_time])
This question already has answers here:
Get the records of last month in SQL server
(23 answers)
Closed 8 years ago.
sql 2005 server
get previous month records
Date product
24-05-2014 ball
25-05-2014 bat
01-06-2014 hat
i need
Date Product
24-05-2014 ball
25-05-2014 bat
declare #ex datetime
set #ex '06-01-2014'
select * from tabl where DATENAME(m,DATEADD(m,0,Date)) =DATENAME(m, DATEADD(m,0, #ex))- it works
select * from tabl where DATENAME(m,DATEADD(m,0,Date)) =DATENAME(m, DATEADD(m,-1,#ex))-not works
My sample code (tested on 2008). I don't know are YEAR and MOTH function in 2005 if not you need to use some string function to extract date / month part from datetime converted to string
declare #ex datetime = '2014-01-01'
declare #prev_year int
declare #prev_month int
set #prev_year = year(dateadd(month, -1, #ex))
set #prev_month = month(dateadd(month, -1, #ex))
select * from tabl
where year(Date) = #prev_year and month(Date) = #prev_month
You're using dd-MM-yyyy format for your dates, which is a varchar in SQL Server.
Therefor, you must use CONVERT :
declare #ex varchar(10)
set #ex = '06-01-2014'
SELECT DATENAME(m,DATEADD(m,0,GETDATE())),
DATENAME(m, DATEADD(m,-1,CONVERT(date, #ex, 103)));
This yields results:
June | May
I think you can figure out your solution from here.
Note: If you use declare #ex datetime , your results will yield June | December