SQL date convert from 20 Dec 2016 to 20 December 2016 - sql

current SQL to get 20 Dec 2016 is:
Convert(char(11), getdate(), 13) DATE,

SELECT FORMAT(cast('20 DEC 2016' as date),'dd MMMM yyyy')

Related

Format string in to Date Sql

I am trying to convert
Thu Jan 22 07:10:00 CST 2015
into date format of mm/dd/yyyy => 01/22/2015.
I tried using the convert function with datetime and format types:
select convert(varchar(11, 'Thu Jan 22 07:10:00 CST 2015', 109)
but I get an error.
Is the error because you are missing a bracket?
select convert(varchar(11), 'Thu Jan 22 07:10:00 CST 2015', 109)
^
i had it solved by following sql. posting here so it will help someone in need
select convert(varchar(11),convert(datetime,Right('Thu Jan 22 07:10:00 CST 2015',4) +'-'+ SUBSTRING('Thu Jan 22 07:10:00 CST 2015',5,3)+'-'+substring('Thu Jan 22 07:10:00 CST 2015',9,2)),101) DateValue

How to get data from Jan to last month in current month

I have a requirement where I need the data from Jan to last month, so for Dec 2017 I want the data from Jan 2017 - Nov 2017 , and in Jan 2018 I want the data from jan 2017 - Dec 2017, in feb 2018 I want the data from 1st Jan 2018- 31st Jan 2018, in March I want Jan 2018 - Feb 2018 and so on.
Below is my code:
(case when (DateFilled) between cast (DATEADD(YEAR, DATEDIFF(YEAR, '19000101', '2018-01-05'), '19000101') as datetime)
and cast (DATEADD(D, -1, DATEADD(MONTH, DATEDIFF(MONTH, '19000101', '2018-01-05'), '19000101')) as datetime)
THEN 1 else 0 end) as numeric(10,2) [TillLastMonth]
(Datefilled is the column with datatime datatype)
Any help is appreciated!!
Thanks

How to subtract 30 days from the current date using SQL Server

I am unable subtract 30 days from the current date and I am a newbie to SQL Server.
This is the data in my column
date
------------------------------
Fri, 14 Nov 2014 23:03:35 GMT
Mon, 03 Nov 2014 15:18:00 GMT
Tue, 11 Nov 2014 01:24:47 GMT
Thu, 06 Nov 2014 19:13:47 GMT
Tue, 04 Nov 2014 12:37:06 GMT
Fri, 1 Nov 2014 00:33:00 GMT
Sat, 5 Nov 2014 01:06:00 GMT
Sun, 16 Nov 2014 06:37:12 GMT
For creating the above column I used varchar(50) and now my problem is I want to display the dates for past 15-20 days from the date column can any one help with this issue ?
update [ how can i display last 7 days dates in order
You can convert it to datetime, and then use DATEADD(DAY, -30, date).
See here.
edit
I suspect many people are finding this question because they want to substract from current date (as is the title of the question, but not what OP intended). The comment of munyul below answers that question more specifically. Since comments are considered ethereal (may be deleted at any given point), I'll repeat it here:
DATEADD(DAY, -30, GETDATE())
Try this:
SELECT GETDATE(), 'Today'
UNION ALL
SELECT DATEADD(DAY, 10, GETDATE()), '10 Days Later'
UNION ALL
SELECT DATEADD(DAY, –10, GETDATE()), '10 Days Earlier'
UNION ALL
SELECT DATEADD(MONTH, 1, GETDATE()), 'Next Month'
UNION ALL
SELECT DATEADD(MONTH, –1, GETDATE()), 'Previous Month'
UNION ALL
SELECT DATEADD(YEAR, 1, GETDATE()), 'Next Year'
UNION ALL
SELECT DATEADD(YEAR, –1, GETDATE()), 'Previous Year'
Result Set:
———————– —————
2011-05-20 21:11:42.390 Today
2011-05-30 21:11:42.390 10 Days Later
2011-05-10 21:11:42.390 10 Days Earlier
2011-06-20 21:11:42.390 Next Month
2011-04-20 21:11:42.390 Previous Month
2012-05-20 21:11:42.390 Next Year
2010-05-20 21:11:42.390 Previous Year
TRY THIS:
Cast your VARCHAR value to DATETIME and add -30 for subtraction. Also, In sql-server the format Fri, 14 Nov 2014 23:03:35 GMT was not converted to DATETIME. Try substring for it:
SELECT DATEADD(dd, -30,
CAST(SUBSTRING ('Fri, 14 Nov 2014 23:03:35 GMT', 6, 21)
AS DATETIME))
SELECT DATEADD(day,-30,date) AS before30d
FROM...
But it is strongly recommended to keep date in datetime column, not varchar.

Changing date format for PostgreSQL query result

I have following query
select substring(listDate from '............$') as v_end_date,
substring(listDate from '^...............') as v_start_date
Now listDate value can be like
select substring('06 Jan 2014 to 12 Jan 2014,
13 Jan 2014 to 19 Jan 2014,
20 Jan 2014 to 26 Jan 2014
' from '............$') as v_end_date,
substring('06 Jan 2014 to 12 Jan 2014,
13 Jan 2014 to 19 Jan 2014,
20 Jan 2014 to 26 Jan 2014
' from '^............') as v_start_date
Above query results in
V_END_DATE V_START_DATE
26 Jan 2014 06 Jan 2014
Now I need to have v_end_date and v_start_date format like yyyy-mm-dd and like
Mon 06 Jan 2014.
Convert your string to an actual date with to_date() and use to_char() to get pretty much any format you like.
Demo:
SELECT to_char(day, 'YYYY-MM-DD') AS format1
, to_char(day, 'Dy DD Mon YYYY') AS format2
FROM (SELECT to_date('26 Jan 2014', 'DD Mon YYYY') AS day) sub

Extract values from IN parameter and store it in local variables in postgreSQL

I have :listDate:String IN parameter that I'm passing to my proc which contains the dynamic values separated by ,
This :listDate:String is passed to proc from Java front end and it contains the value selected by the user
There can be many combinations
Case 1 listDate can have
1. 30 Dec 2013 to 05 Jan 2014
so v_start_date is 30 Dec 2013 and
v_end_date is 05 Jan 2014
Case 2 listdate can have
30 Dec 2013 to 05 Jan 2014,
06 Jan 2014 to 12 Jan 2014
so v_start_date is 30 Dec 2013 and
v_end_date is 12 Jan 2014
Case 3 listDate can have
06 Jan 2014 to 12 Jan 2014,
13 Jan 2014 to 19 Jan 2014,
20 Jan 2014 to 26 Jan 2014
so v_start_date is 06 Jan 2014 and
v_end_date is 26 Jan 2014
How can I extract the values as shown above into v_start_date and v_end_date ?
I was able to resolve it by using
select substring(listDate from '............$') as v_end_date,
substring(listDate from '^...............') as v_start_date