I am trying to take a date which is in a varchar column in a table, add 1 day to it, and set it as the value of a datetime variable. This is part of a process that runs daily and I need to make sure the day resets to one at the end of the month. Or at the end of the year it doesn't increase from 151231 to 151232. The problem I am having is converting #Date back to numeric in the form YYMMDD. For example VIRN_CHK = '151231', #Date as written below is 'Jan 1 2016 12:00AM'. I need to convert it to 160101 so I can save it in a column in another table of type numeric(6,0).
DECLARE #Date as datetime
set #Date = convert(varchar,dateadd(d, 1,(select top(1) VIRN_CHK from STAGE_INST)))
update cdcdatei
set OT_DATE = #Date
This will work by rebuilding the string format
SELECT RIGHT(YEAR('2015-11-01'),2)
+ RIGHT('00' + CAST(MONTH('2015-11-01') AS VARCHAR(2)),2)
+ RIGHT('00' + CAST(DAY('2015-11-01') AS VARCHAR(2)),2)
So you need to extract the year, month and day numbers from the date? You can to that using DATEPART
DATEPART(yy,dateadd) AS DateYear,
DATEPART(mm,dateadd) AS DateMonth,
DATEPART(dd,dateadd) AS DateDay
then you can multiply and sum them to obtain what you need
VIRN_CHK = DateDay + DateMont * 100 + DateYear * 10000
Related
It must be very simple, but I don't know SQL language very well.
I need to filter data by date which is in this format:
How to do it right to filter data this way?
FROM [TableName] where
FileDate>=20220505
I've already tried the command LEFT and CAST but with no success
Something like this may work:
declare #now Datetime = getdate();
declare #intNow int = cast(cast(datepart(year, #now) as varchar(4)) + RIGHT('00'+CAST(datepart(month, #now) AS VARCHAR(2)),2) + RIGHT('00'+CAST(datepart(day, #now) AS VARCHAR(2)),2) as int)
Although if you have your date to check against in the right format e.g. using:
declare #dateToCheck Datetime = cast(cast(20220505 as varchar) as datetime)
And then
FileDate>= #dateToCheck
it should work
You can create an integer representation of your datetime by multiplying and adding the date parts:
year * 10000 20220000
month * 100 500
day 5
-------------------------
20220505
...
FROM [TableName]
WHERE (DATEPART(year, [FileDate]) * 10000) + (DATEPART(month, [FileDate]) * 100) + (DATEPART(day, [FileDate])) >= 20220505
However I'd still look into fixing the condition input format instead.
Credit to #Rangani in Yesterday's date in SSIS package setting in variable through expression for "multiply and add instead of string concat" trick
I am converting a date using CONVERT(varchar,DateOfBirth,101) for birthdates.
I want to show these dates with the current year, I've tried REPLACE but you can't use wildcards with it and when I use DATEPART, it doesn't format with the right digits for month and day. I also can't add years because they are wildly different birthdates. Thanks.
If you want to display the date as a string in 101 format for current year, one option uses a direct format():
format(DateOfBirth, 'MM/dd/2020')
You can compute the current date dynamically:
format(DateOfBirth, concat('MM/dd/', year(getdate())))
On the other hand, if you want your result as a date, then you could use datefromparts():
datefromparts(year(getdate()), month(DateOfBirth), day(DateOfBirth))
If it is a datevalue, you can use FORMAT function. If it is a character value, you can use RIGHT and REPLACE.
DECLARE #dateValue DATETIME = '05/12/1999'
DECLARE #dateCharValue VARCHAR(12) = '05/12/1999'
SELECT FORMAT(#dateValue, 'MM/dd/2020')
SELECT REPLACE(#dateCharValue, RIGHT(#dateCharValue,4),2020)
--Result
05/12/2020
This could helped you:
The code CONVERT(varchar(5),GETDATE(),1) return this 05/27 and then just add the year of the date
SELECT CONVERT(varchar(5),GETDATE(),1) + '/' + cast(year(getdate()) as varchar)
Or
SELECT CONVERT(varchar(5),GETDATE(),1) + '/' + convert(varchar,year(getdate()))
The result of both:
05/27/2020 --(This is my current date n.n )
This work but if you use a string something like your example DateOfBirth will be the variable and if this is a string (DateOfBirth = '5/27/1987') you need to convert the string DateOfBirth to Date:
SELECT CONVERT(varchar(5),convert(date,DateOfBirth),1) + '/' + cast(year(GETDATE()) as varchar)
Or
SELECT CONVERT(varchar(5),convert(date,DateOfBirth),1) + '/' + convert(varchar,year(GETDATE()))
The Result of Both :
05/27/2020
Actually I have different date in SQL table when I pull those via SQL query, day of datetime field should have fixed day.
Example: (DD-MM-YYYY) day should be "7" > (7-MM-YYYY)
10-08-2007 > 07-08-2007
27-12-2013 > 07-12-2013
01-03-2017 > 07-03-2017
Can someone help me on this. Thanks in Advance.
Find the difference between 7 and the day of the original date and add that to the original date:
SELECT DATEADD(DAY, 7 - DAY(OriginalDate), OriginalDate)
Use DATEPART to take out the month and year parts. Cast those into varchar and concatenate with 07.
Query
select '07-' +
cast(DATEPART(mm, [date_column]) as varchar(2)) + '-' +
cast(DATEPART(yyyy, [date_column]) as varchar(4))
from your_table_name;
Assuming You might have to change the day number example
DECLARE #dayNum char(2)
SELECT #dayNum = '07'
select #dayNum + Right(convert(char(10),getdate(),105),8)
If that is not the case You could do this
select '07'+ Right(convert(char(10),'10-08-2007',105),8)
I'd go this way:
SELECT CONVERT(DATE,CONVERT(VARCHAR(6),GETDATE(),112)+'25',112);
CONVERT with format 112 will return the date as unseparated ISO (today we would get 20170407). Convert this to VARCHAR(6) will implicitly cut away the day's part (201704).
Now we add a day and use again CONVERT with 112, but now with DATE as target type.
One thing to keep in mind: The day you add must be two-digit. You can achieve this with
DECLARE #int INT=7;
SELECT REPLACE(STR(#int,2),' ','0');
Use DATEFROMPARTS: Updated ONLY works from 2012 - OP has tagged SQL-Server 2008
select DATEFROMPARTS ( year('10-08-2007'), month('10-08-2007'), 7 )
Assuming that your field is of datetime datatype and your fixed day is of integer type.
select datetimecolumn+(yourparamfixedday-datepart(dd,datetimecolumn))
I have a table which has PlanTotal (how many plans), PlanStartDate (start date of plan) and PlanInstallment columns which tell me how many monthly payments the plan has left.
What coding would I need to work out
the date the plan will end and
how many plans are due to finish after today's date?
I also need to factor in that the PlanStartDate is in numeric value not datetime, so would need to convert this as well in the code.
Any help/ advice appreciated!
Here are many ways to format dates. I took GetDate() as the exemple.
DECLARE #NumericDate AS INT
SET #NumericDate = CAST(CONVERT(CHAR(10),GETDATE(),112) AS INT)
SELECT GETDATE() Today,
CONVERT(INT,GETDATE(),112) DateInt,
CAST(CONVERT(CHAR(10),GETDATE(),112) AS INT) DateNumeric,
YEAR(GETDATE())*10000 + MONTH(GETDATE())*100 + DAY(GETDATE()) AS DateNumeric_V2,
CONVERT(CHAR(10),GETDATE(),121) DateChar,
DATEADD(day,DAY(GETDATE())*-1,DATEADD(MONTH,1,GETDATE())) AS LastDayOfCurrentMonth,
SUBSTRING(CAST(#NumericDate AS CHAR(8)),1,4)
+ '-' + SUBSTRING(CAST(#NumericDate AS CHAR(8)),5,2)
+ '-' + SUBSTRING(CAST(#NumericDate AS CHAR(8)),7,2) AS NumericDate_2_String
And for the end date calculation you have DATEADD()
DECLARE #PlanInstallment as int
DECLARE #PlanStartDate AS INT
SET #PlanInstallment = 4
SET #PlanStartDate = 20140317
SELECT #PlanStartDate AS PlanStartDate,
DATEADD(MONTH,#PlanInstallment,GETDATE()) AS PlanEndDate
So with a mix of those 2 queries you should be able to do anything you want.
Basically I have 3 separate columns in a table. I will call them SMonth, Sday, Syear. They are stored as numeric values for some reason. I can use the following string to format them into what looks like a date but doesn't allow me to use functions such as sort, order by, datediff or dateadd.
CAST(SMonth AS varchar(2)) + '/' + CAST(SDay varchar(2)) + '/' + CAST(SYear AS varchar(4))
Anyone know how to convert this into a workable date, without changing the table?
It doesn't matter how it looks as long as I can use it ie a date or datetime makes no difference.
Thanks in advance.
Just convert your result into a date or datetime.
DECLARE #SMonth AS INT = 12
DECLARE #SDay AS INT = 31
DECLARE #SYear as INT = 2013
SELECT CONVERT(DATE,CAST(#SMonth AS varchar(2)) + '/' + CAST(#SDay AS varchar(2)) + '/' + CAST(#SYear AS varchar(4)))
you should convert format the string as yyyy/mm/dd in order to make sure that SQL Server uses ODBC canonical format
SELECT CONVERT(date, CONVERT(varchar, Syear) + '/' + convert(VARCHAR, SMonth) + '/' + convert(VARCHAR, SDay ) )
Otherwise it your results could depend on the default dateformat or someone changed it using SET DATEFORMAT, for example:
05/10/2013 could mean
May 10, 2013 if the DATEFORMAT is U.S.
October 5, 2013 if the DATEFORMAT is Brithish / French
see this for complete reference of dateformat