Format time value as HH:MM:SS - sql-server-2008

How I may be able to Trim down Time DataType when retrieving from the database?
Value from the Database : 08:00:00.0000000
What I need : 08:00:00 only
I'm SQL Server 2008, VB.Net 2010

You're not really trimming (that's removing leading or trailing spaces), but any of these should work in SQL:
SELECT CAST(#YourValue as time(0))
or
SELECT LEFT(#YourValue, 8)
or
SELECT CAST(#YourValue as char(8))

Related

MySQL STR_TO_DATE Problem while using this function

As title, I'm trying to convert a VARCHAR column in a DATE column, and data is populated in that format "DDMMYYYY" ex. XMAS is "25122022" and in this case the correct formula should be STR_TO_DATE(column, '%d%m%Y')Well, when I execute this query I get an error since in some cases I have values with a "missing" char, I mean, for example, "1012023" when the day is <10 the query fails, cause it checks for "01122023" instead.I could solve this easily by adding a 0 to all fields having length 7, but I'd like to make it more clean.Reading better the usage of STR_TO_DATE I noticed that I could replace %d with %e since the second choice should theorically consider days from 0 to 31 instead of 01 to 31.Unexpectedly the query didn't work and gave me the same erorr at the first instance of a length 7 string.Am I doing something wrong?Thanks in advance.
We can try left padding your date string with zero to a length of 8:
WITH yourTable AS (
SELECT '1012023' AS dt
)
SELECT STR_TO_DATE(LPAD(dt, 8, '0'), '%d%m%Y') AS dt_out -- 2023-01-01
FROM yourTable;
Demo

Numeric column to datetime in SQL

I have a numeric column in my 'daily call' dataset which has a numeric format that I would like to convert into a DATE format.
The problem is, the column has a single data of '81121' (INT) which basically should convert to Aug 11, 2021. however when I use DATE(column name), it converts it to Nov 21, 2008 (CAST gives me the same result).
I have tried CONVERT as well but no luck.
Can someone please advise?
Note: The '11' part in 81121 is the date which changes everyday (looking for something scalable)
Regards,
S
This should do it:
SELECT STR_TO_DATE(LPAD(81121, 6, '0'), '%m%d%y')
-- note that %m expects 2 digits so LPAD is necessary
However I would suggest storing dates using the date datatype.

SQL Server Compact Edition 12 hour time format

Been searching anywhere but I cant find any solution to my problem. I am working with SQL Server Compact Edition, Im using SQL Management Studio 2008 and dealing with time formats.
I want a datetime datatype returns a 12hour format. So far, I have this one:
SELECT
CONVERT(nvarchar(10), StartTime, 108) as timein
FROM TicketSales
It gives this result : 14:43:05
How can I achieve a 12 Hour format for this ? any help please?
Note: For SQL Server Compact Edition 2008
The closest I could come to an answer uses format mask 109:
SELECT
GETDATE() AS timestamp,
RIGHT(CONVERT(nvarchar(26), GETDATE(), 109), 14) AS time;
If you don't want those fractional seconds, then we can try using substring and concatenation:
WITH cte AS (
SELECT CONVERT(nvarchar(26), GETDATE(), 109) AS time
)
SELECT
time,
SUBSTRING(time, 13, 8) + RIGHT(time, 2) AS time
FROM cte;
You must use the right style, like 100
declare #mytime as Time = '14:00'
select CONVERT(varchar(15),#mytime,100)
=> 2:00pm
Original Answer:
converting-a-time-into-12-hour-format-in-sql

How to insert date in a table with a different format?

My problem here is that I have a date value as 05/09/2013 in a HTML text box which cannot be altered.
Now sql only accepts date format as yyyy/mm/dd. so while inserting this textbox value into sql database it does not support this format and throws an exception.
Therefore my question is how to insert a textbox value with different format into a sql database?
For instance my code would look like
insert into table (date) value('"& date& "')
input box has a calender type textbox and sends a date in 05/09/2013 format
UPDATE
For MySQL use STR_TO_DATE()
INSERT INTO Table1 (date)
SELECT STR_TO_DATE('05/09/2013', '%d/%m/%Y')
SQLFiddle
Based on your comments the query should look like
sql = "INSERT INTO shiftpatterns (siteNumber,shiftdate) SELECT '"&siteNumber&"', 'STR_TO_DATE('"&shiftdate&"','%d/%m/%Y')"
Original answer
For SQL Server use CONVERT
INSERT INTO Table1 ([date])
SELECT CONVERT(datetime, '05/09/2013', 103)
SQLFiddle
In both cases
SELECT * FROM table1
will give you:
| DATE |
------------------------------------
| September, 05 2013 00:00:00+0000 |
According to your tags you're using classic ASP, so we can assume that you're using a form to submit the data to the ASP page you're writing.
The most fault-tolerant way, if you cannot edit the client-side page, is to spool up a date object, parse the supplied value, and then extract your data from there. Or, you could just treat it as a string, test for the expected format, and then maniplate the string into the clearer form.
function fixDate(inStr)
if inStr like '99/99/9999' then
fixDate = LEFT(inStr,4) + "/" + RIGHT(inStr,4)
endif
end function

mySQL format number output , thousands separator

I have a mySQL query which is outputting decimal fields with a comma.
SELECT Metals.Metal, FORMAT(Fixes.GBPam, 3) AS AM, FORMAT(Fixes.GBPpm, 3) AS PM,
DATE_FORMAT(Fixes.DateTime, '%d-%m-%y') AS Date
FROM Fixes, Metals
WHERE Metals.Id = Fixes.Metals_Id
Fields GBPam and GBPpm are both of type decimal(10,5)
Now I want columns AM and PM to be formatted to 3 decimal places in my sql query - Correct
I want values in the thousands to be formatted as xxxx.xxx and not x,xxx.xxx - Incorrect
Example output from mysql query:
Metal AM PM Date
Gold 1,081.334 NULL 11-09-12
Silver 21.009 NULL 10-09-12
Platinum 995.650 NULL 11-09-12
Palladium 416.700 NULL 11-09-12
Can you see that output for Gold AM is 1,081.334? How can I get it to output 1081.334?
This is a pain in the ass for me because I have to then muck about in PHP to remove the comma. I would prefer to just get mysql to format it correctly.
Just use ROUND, this is a numeric function. FORMAT is a string function
ROUND(Fixes.GBPam, 3)
you can use replace command for this purpose.
REPLACE(Fixes.GBPam,',','')
EDIT:
With respect to your question you could do something like this:
SELECT Metals.Metal, ROUND(REPLACE(Fixes.GBPam,',',''),3) AS AM,
ROUND(REPLACE(Fixes.GBPpm,',',''),3) AS PM,
DATE_FORMAT(Fixes.DateTime, '%d-%m-%y') AS Date
FROM Fixes, Metals
WHERE Metals.Id = Fixes.Metals_Id
Use replace function. Whether the field is integer or varchar, it will work.
select replace(Fixes.GBPam,',','.');