DATE BETWEEN issues [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Trying to select data only between a range.
select ITM_NBR,
TOT_IVO_ITM_QTY,
Count(*)
FROM dataset
WHERE
bus_dt BETWEEN '2-14-2020' AND '2-15-2021'
Failed conversion to numeric value. Tried without single ticks and it returned zero rows which makes me believe that the column is stored as a VARCHAR. Tried Casting the bus_dt column into a date format.
CAST(bus_dt AS DATE FORMAT 'mm/dd/yyyy') BETWEEN 2/14/2020 AND 2/15/2021
Again failed conversion.
I feel as if I’ve tried every combination and can’t get anything to work of casting and putting the date values in yyyy-mm-dd, mm/dd/yyyy, etc. formats.
And also when I HELP VIEW to see the column type I get “?”.
Kind of at a loss right now.

After I thought I tried everything I figured it out....
I was not formatting my date literals correctly .... faceplam
DATE'yyyy-mm-dd'....

Related

Error while converting varchar to smalldatatime [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
Well, I have a problem with sending data. There are three stations, all of which work on-site on an ongoing basis. I checked the system settings and settings and one of the stations was updated to the 21H2 version (I set everything up as it is at the station where it works). Unfortunately, this option did not help. I also turned off the error on the server by setting it on the station.
I'm adding an error dump
I think SQL Server assumes another format than the one you
intended and another value causes the error.
Use CONVERT(smalldatetime, YourColumn, 101) to force format mm/dd/yyyy, or
use CONVERT(smalldatetime, YourColumn, 103) to force dd/mm/yyyy.
To prevent this in the future:
Always store dates as a datetime or smalldatetime column.
When you have to convert character data to datetime, always use one of
the unambiguous formats:
For date only: yyyymmdd
For date plus time: yyyy-mm-ddThh:mm:ss.mmm (where the optional .mmm
part denotes the milliseconds)

Change format and remove nanoseconds from Time in SSMS 18 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am working on a table that will display employees' Shift Start and End times. Also, it will have the employees' Punch In and Punch Out times.
Is there a way to remove the nanoseconds and change the 24-hour Time format to AM/PM? I'm using Microsoft SQL Server Management Studio 18.
Yes, you can format your datetime column using the format function:
SELECT FORMAT(ShiftStartDate, 'yyyy-MM-dd hh:mm:ss tt')
FROM yourtable

SQL Datediff on multiple levels [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a lot of issues using the datediff function in SQL Server.
I have two tables that I want to join based on the time gap between observations; the tables contain arrival times of shipments as follows yyyy-mm-dd hh:mm:ss.
I found a different question of which the answer will (hopefully) help me with the join bit. The code provided by said example states the following, and this is where the struggles begin.
Abs(DateDiff("d", pi.Product_Interest_Date, l.Lead_Date)) AS Date_Gap
Source: Join two tables on the same date or closest date (before or after)
However, I need to find the date gap between values based on the full date, so from year to seconds. Am I correct in understanding that the datediff function only accepts one level (i.e. only day or minute), or am I doing something wrong?
If the former is the case, is there an easy way to do what I want to do?
First, the syntax in SQL Server for days is:
abs(DateDiff(day, pi.Product_Interest_Date, l.Lead_Date)) AS Date_Gap
The syntax that you are using is MS Access syntax.
SQL Server does not have an interval type. You can get the difference in seconds and then convert to another unit. For instance, decimal days is:
abs(DateDiff(day, pi.Product_Interest_Date, l.Lead_Date)) / (24.0 * 60 * 60) AS Date_Gap
You don't provide sample data and desired results that describes the exact results you want, but this should put you on the right track. If this doesn't fully answer your question, then you should ask a new question with sample data, desired results, and a clear explanation of the logic.

Convert varchar to be float [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 2 years ago.
Improve this question
I got this error when trying to convert varchar to float using cast or convert:
Error converting data type varchar to float.
Can someone please help?
You can use try_convert():
select try_convert(float, col)
If this fails, the functions returns NULL but the query continues processing.

Fill with zero to complete a defined number in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to complete cards numbers in sql. I have the prefix =11111 and the number of the card which is variable, therefore it could be '25' or '2130' but at the end I must have 14 numbers. So I need to fill spaces with zeros.
I've read about 'LPAD' but I don't understand very well this method.
You could use lpad, but if you're starting with a number you could use a 9-digit format model instead, and concatenate that onto your prefix:
select '11111' || to_char(25, 'FM000000000') from dual;
11111000000025
The FM format modifier stops Oracle adding a space for a potential +/- sign indicator.
SQL Fiddle demo
Use the ZEROFILL attribute.
But your database should only be responsible for saving data and not changing it before saving.
The best way would be to send the zerofilled data to the database server.