sql-date-functions

Get last day of previous month in yyyy-mm-dd format

无人久伴 提交于 2020-01-06 08:09:12
问题 How do I get the last day of the previous month in yyyy-mm-dd format? This is what I have tried, however I don't want the seconds showing. Only YYYY-MM-DD: SELECT DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0)) 回答1: Use the EOMONTH function along with FORMAT : SELECT FORMAT(EOMONTH(CURRENT_TIMESTAMP, -1), 'yyyy-MM-dd') The -1 in the above example means subtract 1 month from argument 1 (there is no need for DATEADD ). 回答2: Convert it into date only format SELECT convert(date,DATEADD

Select distinct where date is max

家住魔仙堡 提交于 2020-01-06 04:03:27
问题 This feels really stupid to ask, but i can't do this selection in SQL Server Compact (CE) If i have two tables like this: Statuses Users id | status | thedate id | name ------------------------- ----------------------- 0 | Single | 2014-01-01 0 | Lisa 0 | Engaged | 2014-01-02 1 | John 1 | Single | 2014-01-03 0 | Divorced | 2014-01-04 How can i now select the latest status for each person in statuses? the result should be: Id | Name | Date | Status -------------------------------- 0 | Lisa |

SQL Select Records based on current date minus two days

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-03 17:01:52
问题 I have an orders table which contains an order ID, order date and order description. I want to run a select query which captures all orders that have been created in the last two days. so the current date minus two days. from the 14th December, I would want to select all orders where the order date is > 13th December. This needs to use a Get date function to pick up the current date and minus the days. I have tried: select * from orders where orderdate > getdate() - 2 but this is not

How to parse String to java.sql.date

半城伤御伤魂 提交于 2019-12-30 18:42:12
问题 I have a String String s = "01 NOVEMBER 2012"; Then I want parse it to sqlDate. And insert it into the database. Is it possible to parse that string to sqlDate?!?! Yup, sql date format is "yyyy-mm-dd" 回答1: Use SimpleDateFormat to parse String date to java.util.Date java.util.Date utilDate = new SimpleDateFormat("dd MMM yyyy").parse("01 NOVEMBER 2012"); and then convert it to java.sql.Date using millis java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); 回答2: Expanding on Jigar Joshi

how to create ISO-8601 gregorian date table in postgres

故事扮演 提交于 2019-12-29 09:12:30
问题 I'm looking to create a date table in a postgres database. The sample data is expected to look like this: date key = 00001 calendar_date= 1/1/2015 week_num= 1 month_num= 1 month_name= Jan quarter_num= 1 calendar_year= 2015 iso_dayofweek= 4 dayofweek_name= Thursday Is there a function or SQL that I can get help with to create a date gregorian ISO-8601 table? I am looking to auto generate this if possible. Any help in this direction would be appreciated. 回答1: see the below example SELECT mydate

Script to filter by most recent end of week date

爱⌒轻易说出口 提交于 2019-12-24 08:38:50
问题 My company's reporting week is Monday through Sunday. On Monday morning's I run several queries using Microsoft SQL Server Management Studio to report on business activity for the previous week. I currently use declaration statements to pull the desired date range. This works quite well as long as I'm running reports on Monday. However, if Monday was a holiday and I'm not running reports until Tuesday, I need to manually modify my date range for each query. How can I modify my date filter to

Difference between two dates in postgresql

穿精又带淫゛_ 提交于 2019-12-20 04:16:36
问题 Function: CREATE FUNCTION diff(d1 date,d2 date) RETURNS int AS $$ BEGIN IF d1 = NULL THEN RETURN SELECT extract(year from age(current_date,d2)); ELSE RETURN SELECT extract(year from age(d1,d2)); END IF; END $$ language plpgsql; My requirement is to find the difference between two dates in years. So, I write the above function. Here, if d1 is NULL then it is assigned with current date. But, it produce error like as shown below. ERROR: syntax error at or near "SELECT" LINE 1: SELECT SELECT

Date Conversion Issue MS Access to SQL Server

本小妞迷上赌 提交于 2019-12-13 07:35:32
问题 I'm creating a table B from an exisitng table A. In the table A I have a column ValDate which is varchar and contains Date. When I try to create the table B I have a function used in the query as given below and I get a conversion error. Table A contains null values as well. MS Access: ((DateDiff("d",Date(),format(Replace(Replace([Table A].ValDate,".","/"),"00/00/0000","00:00:00"),"dd/mm/yyyy")))>0)). Tables were in MS Access and are being migrated to SQL Server 2012. SQL Server: ((DATEDIFF

Difference between dates - sql server

江枫思渺然 提交于 2019-12-12 19:18:10
问题 I need to get the following result between two dates: date_start = 01/01/2010 date_end = 10/21/2012 result: 1 year, 9 months and 20 days. I tried the code bellow, but it didn't work. It returns negative dates sometimes: SELECT CAST(DATEDIFF(yy, date_start, date_end) AS varchar(4)) +' year '+ CAST(DATEDIFF(mm, DATEADD(yy, DATEDIFF(yy,date_start , date_end), date_start), date_end) AS varchar(2)) +' month '+ CAST(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, DATEADD(yy, DATEDIFF(yy, date_start, date_end

Change date format to dd/mm/yyyy in sql

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 02:29:26
问题 I have a table called users and In sql the format of date is yyyy-mm-dd there fore when I try to enter data from my website in dd/mm/yyyy format it just enters 0000--00-00 How do I change the format it sql? 回答1: SQL Server Example Link below has an easy explanation and there's an example if it helps SELECT convert(varchar, getdate(), 103) – dd/mm/yyyy Or try putting your column name in place of "datecolumn" CONVERT(varchar(19), datecolumn, 103) 回答2: in SQL Server, according to the article