How to decrement dates? [duplicate] - sql

This question already has answers here:
Difference of two date time in sql server
(21 answers)
Closed 7 years ago.
I have 2 dates: startDate= 6/22/2015 and endDate= 6/20/2015.
I need to decrement these 2 dates, i meen endDate-startDate, so in this example I will get 2.
How can I do this in sql?

The native way to do this would be to use the setDate method. There is an existing Stackoverflow post here: How to Add Dates
However, I always pass along the advice to use a date library, something like moment.js. In most languages, managing dates (timezones, leap years, etc ets) is incredibly difficult, and javascript can be even more so with browser time zone behaviors.

Related

(Databricks SQL) Date stored as STRING (yyyyMMDD) cannot convert it into DATE (yyyy-MM-DD) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I've spent last 3 hours googling the issue but nothing seems to work in Databricks SQL version specifically. I have to use a database, where someone decided it's best to store date as a STRING, there's no way around it short-term.
Current date format (STRING) is following:
yyyyMMDD
What I want it to be is (DATE):
yyyy-MM-DD
I tried CAST, I tried CONVERT, I tried 5 different solutions from stack and other website but the code seems to be written for SQL Dev where there are more functions available and I am forced to work with Databricks. I'd be grateful for any help!
tx!
I know nothing about Databricks, but after some googling, it looks like there is a MAKE_DATE function that can help you. You can extract the year, month, and day values from your YYYYMMDD string. It might look something like this, if your date string is called ds:
MAKE_DATE(
SUBSTRING(ds FROM 1 FOR 4),
SUBSTRING(ds FROM 5 FOR 2),
SUBSTRING(ds FROM 7 FOR 2)
)
If you want the code to be even more transparent, you can cast the substrings to integers yourself instead of relying on implicit conversion for the arguments to MAKE_DATE.

I'm currently looking for a dynamic way to convert rows to columns in a specific way in SQL server [duplicate]

This question already has answers here:
T-SQL dynamic pivot
(5 answers)
Closed 6 years ago.
I'm currently looking for a dynamic way to convert rows to columns in a specific way in SQL server (I was able to do it in excel vba but the excel limitations made me go to SQL).
Abstract : I am making a daily analysis over 10 years considering 1315 stocks, for each stock we have daily returns for a period going from 29/12/2009 to 30/12/2016.
As you can see every 2614 row there's a new stock with the 3 following rows showing text..
Table on SQL
And I would like to obtain this result.. therefore looking for a good insight to help me go through this!
Desired solution draft
I am doing this for a quantitative department in Luxembourg to implement a dynamic model allocation of smart betas. (First time with SQL)
Thank you for your help! feel free to ask any questions if you need any other detail..
R.H.
This is also known as a dynamic pivot -- for sql server see https://stackoverflow.com/a/10404455/1327961

SQL Server parsing function? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Split Function equivalent in tsql?
I have a column that contains data in the form:
CustomerZip::12345||AccountId::1111111||s_Is_Advertiser::True||ManagedBy::3000||CustomerID::5555555||
Does SQL have any sort of built in function to easily parse out this data, or will I have to build my own complicated mess of patindex/substring functions to pull each value into its own field?
I don't believe there is anything built in. Look at the comments posted against your original question.
If this is something you're going to need on a regular basis, consider writing a view.

Situation with SQL query [duplicate]

This question already has answers here:
How to search for a comma separated value
(3 answers)
Closed 8 years ago.
I have data in table in below format
id brand_ids
--------------
2 77,2
3 77
6 3,77,5
8 2,45,77
--------------
(Note the brand ids will be stored like comma separated values, this is common for values in this field)
Now i am trying to get a query which is capable of querying out only rows which have '77'
in it..
I know i can use LIKE command in three formats LIKE '77,%' OR LIKE '%,77,%' OR LIKE '%,77' with or condition to achieve it. But i hope this will increase the load time of the sql.
is there any straight forward method to achieve this? if so please suggest.
Thanks,
Balan
A strict answer to your question would be: no. Your suggestion of using LIKE is your best option with this data model. However, as mentioned, it is highly suggested that you use a more normalized model.

How to filter datetime values in past 24 hours in HQL?

I haven't found a clear answer but I would like to grab any values from within the past 24 hours. I have an alternative solution in code but I would like to see if there is an equivalent to using t-sql datediff
You don't need datediff, because you already know what time it was 24hs ago.
Here's a Linq (NH 3.x) example:
session.Query<Foo>()
.Where(f => f.DateAndTime >= DateTime.Now.AddDays(-1))
If you use HQL, you can get the DB server time with standard functions like current_timestamp (left as an exercise, but I'll add that if you need it)