I need to convert this string 2020121 as 2020-04-30 in SQL Server?
How do I do that?
I think that your use of Julian date is the number of days (minus 1) since the start of the year. If so, you can use:
select dateadd(day,
convert(int, right(string, 3)) - 1,
datefromparts(convert(int, left(string, 4)), 1, 1)
)
Here is a db<>fiddle.
Related
I'm using SQL Server 2014, and I'm trying to perform a data conversion, the value being passed '04-SEP-44'
Since this was the birth year, it needs to be 1944, not 2044.
I've tried
select convert(DATETIME, '04-SEP-44')
select convert(DATETIME, '04-SEP-44', 1)
select convert(DATETIME, '04-SEP-44', 13)
But they are all using the year as 2044.
Are there any quick fixes to this issue?
Possibilities:
If the value is greater than today subtract 100 years...
Force "19" into the string...
But it really depends on your business rules, is it always going to be 1900, or could some dates be early 2000's? You should really update your front end to capture the full date.
select case when Y.[Value] > getdate() then dateadd(year, -100, Y.[Value]) else Y.[Value] end
, convert(date, substring(X.[Value], 1, 7) + '19' + substring(X.[Value], 8, 2))
from (values ('04-SEP-44')) as X ([Value])
cross apply (values (convert(date, X.[Value]))) as Y ([Value]);
Note: why would you convert a date to a datetime? Don't use a datetime if you don't have a time component.
I Need help in finding upcoming birthday in a month.
My Data is something like below , Both data types are nvarchar
Could anyone help me with the sql query please? how to set the DOB column into a date format and then find the birthday with month as 11 and date as 24.
Thanks in advance
Assuming SQL Server, you can use month() to extract the month from a date, for example getdate(), which is the current point in time. With left() you can extract the first characters of a string. That leads to something like:
SELECT [Name],
[Dob(mmdd)]
FROM elbat
WHERE month(getdate()) = left([Dob(mmdd)], 2);
In Microsoft SQL Server you can Create a date using the DATEFROMPARTS(int year, int month, int day) function. To get your month and day you would have to get the 2 parts of the string, the first 2 characters for month and the third and fourth characters as the day, you can use the SUBSTRING function for this. Then take each pair of characters for month and day and cast to int and use them in the DATEFROMPARTS function.
Then you want to see if your newly created date is BETWEEN today AND one month from today. So you could do something like this:
SELECT *
FROM SomeTable
WHERE
DATEFROMPARTS(YEAR(GETDATE()), CAST(SUBSTRING([Dob(mmdd)], 1, 2) as INT), CAST(SUBSTRING([Dob(mmdd)], 3, 2) as INT))
BETWEEN
DATEADD(DAY, -1, GETDATE()) AND DATEADD(MONTH, 1, GETDATE())
Note: this assumes [Dob(mmdd)] is always 4 characters.
You don't need the DOB in a date format. I am unclear what "upcoming" month means, but I suspect that it means a calendar month. If the current month, then:
where month(getdate()) = cast(left(dob, 2) as int)
If the next month, then:
where month(dateadd(month, 1, getdate())) = cast(left(dob, 2) as int)
Thanks, Everyone for the help.. I got this working,. both works perfect
select [USER_ID],[EMP_FULL_NM],[Birthday_Date] from [dbo].[COE]
where month(getdate())=left([Birthday_Date],2)
select [USER_ID],[JOINING_DT],[EMP_FULL_NM] from [dbo].[COE]
where SUBSTRING(CONVERT(VARCHAR(10), [JOINING_DT], 101),1,2) = month(getdate())
I have the following string 2015089 or 2016075, for example.
I need to get the result in yyyy/mm/dd format based on the given input.
So, based on 2015089, I get, 2015/mm/dd. dd is a 89th day of 2015 and mm is a month that has 89th day.
How can I do something like that?
I think the simplest way is to convert to a date using dateadd():
select dateadd(day, right(str, 3) - 1, datefromparts(left(str, 4) + 0, 1, 1) )
That is, add one less than the number of days to the beginning of the year. This assumes that Jan 1 is represented as "1" and not "0".
You can then format the date however you like.
In pre-SQL Server 2012, you can do:
select dateadd(day, right(str, 3) - 1, cast(left(str, 4) + '0101' as date))
I am running views against a table that has dates stored as varchar(8) as DDMMYYYY, can someone please tell me how do I convert them to date format?
Thanks
In SQL Server 2012+, you can use datefromparts():
select datefromparts(right(col, 4) + 0, substring(col, 3, 2) + 0, left(col, 2) + 0)
I just added the + 0, because I'm not 100% sure if SQL Server will convert the arguments to integers.
Of course, you can also do it the "old" way as well:
select convert(date, right(col, 4) + substring(col, 3, 2) + left(col, 2))
SQL Server recognized the format YYYYMMDD as a valid date.
I want to split date in column in 3 fields, I use this query
SELECT
SUBSTRING(Account.date, 1, 2) AS "Month",
SUBSTRING(Account.date, 4, 2) AS "Day",
SUBSTRING(Account.date, 7, 4) AS "Year"
FROM Account
Almost all data is in format 02/11/2000, but some of it can be 02/November/2000 or 2/11/2000.
Only common thing is that data separated by /. How can I separate this column using the delimiter?
Surprisingly CAST('2/November/2000' as datetime) works (checked on SQL Server 2008), gives value 2000-11-02 00:00:00.000
SELECT
Month(CAST(Account.date AS DateTime)) "Month",
Day(CAST(Account.date AS DateTime)) "Day",
Year(CAST(Account.date AS DateTime)) "Year",
FROM Account
But as rightly pointed out in comment how do you know if "02/11/2000" is November 2, 2000 or February 11, 2000?
Also the spelling of Month names must be absolutely correct else conversion fails. Since you are storing dates as string there is chance that entry like November , Agust etc could have been made .
You should never store date values as strings.
You can do it this way by using CHARINDEX and SUBSTRING functions
select
LEFT(Account.date, CHARINDEX('/', Account.date) - 1),
SUBSTRING(Account.date, CHARINDEX('/', Account.date) + 1, LEN(Account.date) - CHARINDEX('/', Account.date) - CHARINDEX('/', Account.date, CHARINDEX('/', Account.date)) - 2),
REVERSE(LEFT(REVERSE(Account.date), CHARINDEX('/', REVERSE(Account.date)) - 1))
FROM Account
You can abuse the PARSENAME function slightly here:
SELECT FirstPart = PARSENAME(REPLACE(Account.Date, '/', '.'), 3),
SecondPart = PARSENAME(REPLACE(Account.Date, '/', '.'), 2),
ThirdPart = PARSENAME(REPLACE(Account.Date, '/', '.'), 1)
FROM (VALUES
('02/November/2000'),
('2/11/2000')
) Account (Date);
Will give:
FirstPart SecondPart ThirdPart
02 November 2000
2 11 2000
I would however, highly recommend storing your dates using the appropriate data type!. SQL Server 2012 has the TRY_CONVERT function which can make such conversions easier, but you still need to know what format your string date is in, 2/11/2000 could be the 2nd November, or 11th February depending on your regional settings.
You can use the combination of CharIndex and Substring function in SQL to split the string based on delimiter.
You can check CharIndex examples here SQL Server 2005 Using CHARINDEX() To split a string
and here SQL Server - find nth occurrence in a string
Assuming your database column account.date contains a valid datetime or date value you should using SQLServers date functions like:
select month(getDate()) as "Month",
day(getDate()) as "Day",
year(getDate()) as "Year"
I replaced your column account.date by a getDate() to have some test values. This maps to your SQL in the following way
SELECT
month(Account.date) AS "Month",
day(Account.date) AS "Day",
year(Account.date) AS "Year"
FROM Account
Storing these date values as varchars would be IMHO a design flaw of your database structure. Dates are formatted in multiple ways to text. This is the presentation of your database data. To process your data you would always preprocess your text dates. That is bad practice.
If you have indeed varchar values there are several SO questions like: How do I split a string so I can access item x?.
Try this:
DECLARE #VALUE VARCHAR(100)<BR>
SET #VALUE ='2/11/2000' <BR>
SELECT SUBSTRING(#VALUE,0,CHARINDEX('/',#VALUE,0)),
SUBSTRING(#VALUE,
CHARINDEX('/',#VALUE,0)+1,
(CHARINDEX('/',#VALUE,(CHARINDEX('/',#VALUE,0)+1)) -
CHARINDEX('/',#VALUE,0) - 1)
),RIGHT(#VALUE,4)