I am working with SQL Server and a table containing DOB [Date Of Birth] column.
I have saved a value in table against that column, that is = 'May 5 1988 12:00AM', but I need 'May 5 1900 12:00AM' when I select the column.
I have this basic query:
SELECT CAST(dbo.contact.dob AS VARCHAR) AS DOBFROM dbo.contact
The result is May 5 1988 12:00AM
Any help?
If you are going to replace the custom year then this might help you. Here GETDATE() is your datecolumn.
DECLARE #CustomYear = '1900'
SELECT REPLACE(CAST(GETDATE() AS VARCHAR(MAX)),DATEPART(YEAR,GETDATE()),#CustomYear)
This gives me output as,
Mar 17 1900 4:12AM
A simple date math:
select dateadd(year, -datediff(year, '19000101', getdate()), getdate())
(where getdate() stands for your table's column).
You might have a problem with leap years, though.
I hope I can help you out, i think what you need is:
SELECT DATEADD(YEAR, (1900-(DATEPART(YEAR, dbo.contact.dob))), dbo.contact.dob ) AS DOB FROM dbo.contact
DATEADD and DATEPART will do the trick
Try to add 2 years like
SELECT CAST(DATEADD(year,2,dbo.contact.dob) AS VARCHAR) AS DOB
FROM dbo.contact
This will add you years so you will get for May 5 1988 12:00AM -> added 2 years like May 5 1990 12:00AM
Related
I have table with two datetime columns in SQL Server 2000, I want to update only day and month in column_a with day and month in column_b except year.
For example:
column_a
----------
1/2/2009
1/2/2002
1/2/2016
3/1//1998
11/12/1987
column_b
---------
31/12/2015
11/10/2005
27/6/2017
31/12/2010
31/12/2011
Desired results:
31/12/2009
11/10/2002
27/6/2016
31/12/1998
31/12/1987
Thank you for your help.
Using date literals should work in SQL Server 2000, style 112 returns the date in YYYYMMDD so take the YYYY of one and add to MMDD of the other and convert that to datetime.
update tablex
set columnb = convert(datetime(left(convert(varchar, column_a, 112), 4) +
right(convert(varchar, column_b, 112), 4), 112)
It's a great deal easier in later versions. SQL Server 2000 is dead to me.
Use DATEFROMPARTS ( year, month, day )
update tablex
set columnb = datefromparts(year(column_a),month(column_b), day(column_b))
SELECT * FROM [MarkTSK]
WHERE [MonthlyDt] IS NOT NULL
--AND
--SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
Hello folks. Does anyone know how to correctly write this statement to
display the FIRST of each month? I have an Excel file that I am importing
into a Server using MS SSIS (VisualStudio 2008). The dates are monthly, but the last few months were the 15th June, May, April etc. My intent is to make
all of them show 1st of the month. All the months before January 2015 have been on the 1st of the month.
The SQL Query above is what I wrote in the Excel source Editor.
Thank You
This should always give you the first of the current Month
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0)
to pull back the first of the month for dates in your table something like this should do it
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,[MonthlyDt]),0) FROM [MarkTSK]
WHERE [MonthlyDt] IS NOT NULL
SELECT *, DATEADD('d',-DAY([MonthlyDT])+1, [Date]) AS [MonthD]
FROM [MarkTSK]
WHERE [MonthlyDT] IS NOT NULL
Thank you everyone. The SCRIPT above worked for me. All the dates that were not on the 1st of the month, now shows as the 1st.
Use DatePart to extract the Month and Year adding in the day manually.
Something like:
SELECT Convert(date, DatePart('yyyy', [MonthlyDt]) + DatePart('mm', [MonthlyDt]) + '01' ) FROM [MarkTSK]
WHERE [MarkTSK] IS NOT NULL
(Disclaimer: untested code)
I am trying to add days to the current date and it's working fine but when I add 360 days to the current date it gives me wrong value.
eg: Current Date is 11/04/2014
And I am adding 360 Days to it, it should give me 11/04/2015, but it is showing the same date 11/04/2014. the year is not changing.
Here is my code:
select dateadd(dd,360,getdate())
Just do-
Select (Getdate()+360) As MyDate
There is no need to use dateadd function for adding or subtracting days from a given date. For adding years, months, hours you need the dateadd function.
select dateadd(dd,360,getdate()) will give you correct date as shown below:
2017-09-30 15:40:37.260
I just ran the query and checked:
Dateadd(datepart,number,date)
You should use it like this:
select DATEADD(day,360,getdate())
Then you will find the same date but different year.
From the SQL Server 2017 official documentation:
SELECT DATEADD(day, 360, GETDATE());
If you would like to remove the time part of the GETDATE function, you can do:
SELECT DATEADD(day, 360, CAST(GETDATE() AS DATE));
In SQL Server 2008 and above just do this:
SELECT DATEADD(day, 1, Getdate()) AS DateAdd;
can try this
select (CONVERT(VARCHAR(10),GETDATE()+360,110)) as Date_Result
Two or three ways (depends what you want), say we are at Current Date is
(in tsql code) -
DECLARE #myCurrentDate datetime = '11Apr2014 10:02:25 AM'
(BTW - did you mean 11April2014 or 04Nov2014 in your original post? hard to tell, as datetime is culture biased. In Israel 11/04/2015 means 11April2014. I know in the USA 11/04/2014 it means 04Nov2014. tommatoes tomatos I guess)
SELECT #myCurrentDate + 360 - by default datetime calculations followed by + (some integer), just add that in days. So you would get 2015-04-06 10:02:25.000 - not exactly what you wanted, but rather just a ball park figure for a close date next year.
SELECT DateADD(DAY, 365, #myCurrentDate) or DateADD(dd, 365, #myCurrentDate)
will give you '2015-04-11 10:02:25.000'. These two are syntatic sugar (exacly the same). This is what you wanted, I should think. But it's still wrong, because if the date was a "3 out of 4" year (say DECLARE #myCurrentDate datetime = '11Apr2011 10:02:25 AM') you would get '2012-04-10 10:02:25.000'. because 2012 had 366 days, remember? (29Feb2012 consumes an "extra" day. Almost every fourth year has 29Feb).
So what I think you meant was
SELECT DateADD(year, 1, #myCurrentDate)
which gives 2015-04-11 10:02:25.000.
or better yet
SELECT DateADD(year, 1, DateADD(day, DateDiff(day, 0, #myCurrentDate), 0))
which gives you 2015-04-11 00:00:00.000 (because datetime also has time, right?). Subtle, ah?
This will give total number of days including today in the current month.
select day(getDate())
Add Days in Date in SQL
DECLARE #NEWDOB DATE=null
SET #NEWDOB= (SELECT DOB, DATEADD(dd,45,DOB)AS NEWDOB FROM tbl_Employees)
SELECT DateAdd(5,day(getdate()) this is for adding 5 days to current days.
for eg:today date is 23/08/2018 it became 28/08/2018 by using the above query
Does anyone know how to compare two date and to find out whether the date difference is within 3 months in SQL Server?
Example if I have 2 dates:
Start_Date = '2013-02-01'
End_Date = '2013-04-10'
How can I compare two date and to find out whether the date difference is within 3 months in SQL Server?
Look into using DATEDIFF:
SELECT *
FROM YourTable
WHERE DATEDIFF(month, start_date, end_date) <= 3
SQL Fiddle Demo
MSDN: http://msdn.microsoft.com/en-us/library/ms189794.aspx
Here's the answer using the two dates you provided in your question. I gave it to you two ways. The first column is the comparison of whether it is within 3 months. The second column is simply the result of the SQL Server system function DATEDIFF.
DECLARE #Start_Date DATE = '2013-02-01',
#End_Date DATE = '2013-04-10'
SELECT CASE
WHEN datediff(MONTH, #Start_Date, #End_Date) <= 3 THEN
'True'
ELSE
'False'
END AS [WithinThreeMonths]
, datediff(MONTH, #Start_Date, #End_Date) [Months]
How do I exclude values in a DateTime column that are Saturdays or Sundays?
For example, given the following data:
date_created
'2009-11-26 09:00:00' -- Thursday
'2009-11-27 09:00:00' -- Friday
'2009-11-28 09:00:00' -- Saturday
'2009-11-29 09:00:00' -- Sunday
'2009-11-30 09:00:00' -- Monday
this is the result I'm looking for:
date_created
'2009-11-26 09:00:00' -- Thursday
'2009-11-27 09:00:00' -- Friday
'2009-11-30 09:00:00' -- Monday
Thanks!
When dealing with day-of-week calculations, it's important to take account of the current DATEFIRST settings. This query will always correctly exclude weekend days, using ##DATEFIRST to account for any possible setting for the first day of the week.
SELECT *
FROM your_table
WHERE ((DATEPART(dw, date_created) + ##DATEFIRST) % 7) NOT IN (0, 1)
SELECT date_created
FROM your_table
WHERE DATENAME(dw, date_created) NOT IN ('Saturday', 'Sunday')
Assuming you're using SQL Server, use DATEPART with dw:
SELECT date_created
FROM your_table
WHERE DATEPART(dw, date_created) NOT IN (1, 7);
EDIT: I should point out that the actual numeric value returned by DATEPART(dw) is determined by the value set by using SET DATEFIRST:
http://msdn.microsoft.com/en-us/library/ms181598.aspx
Try the DATENAME() function:
select [date_created]
from table
where DATENAME(WEEKDAY, [date_created]) <> 'Saturday'
and DATENAME(WEEKDAY, [date_created]) <> 'Sunday'
The answer depends on your server's week-start set up, so it's either
SELECT [date_created] FROM table WHERE DATEPART(w,[date_created]) NOT IN (7,1)
if Sunday is the first day of the week for your server
or
SELECT [date_created] FROM table WHERE DATEPART(w,[date_created]) NOT IN (6,7)
if Monday is the first day of the week for your server
Comment if you've got any questions :-)
Calculate Leave working days in a table column as a default value--updated
If you are using SQL here is the query which can help you: http://gallery.technet.microsoft.com/Calculate...
Try this code
select (DATEDIFF(DD,'2014-08-01','2014-08-14')+1)- (DATEDIFF(WK,'2014-08-01','2014-08-14')* 2)