I am using SQL Server 2008. I want to show the right abbreviation of September which is Sept not Sep in the report.
This is my code:
CONVERT(VARCHAR(12), SO.CREATED, 107) AS CREATED
The result is: Sep 5, 2016
But the result should be: Sept 5, 2016
How would I correct my formatting?
This should probably do the trick for you:
SELECT REPLACE(CONVERT(VARCHAR(12),SO.CREATED,107),'Sep','Sept') AS CREATED;
Try REPLACE command, as you need only for Sept month (as per my understanding of the question)!
SELECT (REPLACE (CONVERT(VARCHAR(12), SO.CREATED, 107) ), 'Sep', 'Sept') AS CREATED
Hope this works!
Related
Currently, my start_time column was string type.
I want to convert 8:00 AM to 8:00:00 using MySQL.
I have tried like this but it didn't work SELECT STR_TO_DATE('8:00 AM', '%h:%i %p')
Since you are using Laravel, I recommend to use Carbon.
Carbon is an inherited php class from DateTime what makes you able to format times in any way you want and like.
Related to your question:
SELECT STR_TO_DATE("08:00 AM", "%h:%i %p"); // output: 08:00:00
Works fine, so there is something else that causes your problem, but you are giving us not enough information to help you further.
What db are you using?
What version?
what outpout do you get?
etc.
INSERT INTO fb_public_figure_posts VALUES ('153080620724_10158531267690725',
'https://www.facebook.com/DonaldTrump/videos/10158531267690725/',
'2017-01-23T20:36:07+0000',
'2017-01-24T00:22:49+0000',
'Donald J. Trump',
153080620724,
'b'My family and I will never forget Friday, January 20th, 2017. Thank you!'',
'https://www.facebook.com/DonaldTrump/videos/10158531267690725/',
123471,
8263,
153080620724)
when running this sql sentence, it errors:
1064(42000), "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
'My family and I will never forget Friday, January 20th, 2017. Thank you!'','http' at line 1"
could you please help me for that
The line with the 'My family...'-sentence is a bit weird. This looks like a copy-paste-error. If this field has the type of VARCHAR you have to move the sentance in the simple apostrophe
'My family and I will never forget Friday, January 20th, 2017. Thank you!'
Your line is interpreted as
'b' : VARCHAR field
My family and I will never forget Friday, January 20th, 2017. Thank you! : some SQL-Syntax
'' : VARCHAR field
which can't obviously work.
A working INSERT would look like this:
INSERT INTO fb_public_figure_posts
VALUES (
'153080620724_10158531267690725',
'https://www.facebook.com/DonaldTrump/videos/10158531267690725/',
'2017-01-23T20:36:07+0000',
'2017-01-24T00:22:49+0000',
'Donald J. Trump',
153080620724,
'My family and I will never forget Friday, January 20th, 2017. Thank you!',
'https://www.facebook.com/DonaldTrump/videos/10158531267690725/',
123471,
8263,
153080620724
);
The problem is that you have a single quotation in the string you are trying to insert into your database. The single quotation is missing up your INSERT statement. Before putting the text into your INSERT statement, you need to escape any single quotation.
Refer to this previous post "How to escape apostrophe (') in MySql?"
Your sql is not right.'b'My family and I will never forget Friday, January 20th, 2017. Thank you!'', 'b'M...the ' is unnecessary.and ...Thank you!'' the '' is unnecessary.Hope that helps.
it's give error because sql syntax not proper it you insert data with some special character like " ' " so it's need to slashes before special character for example if you add
b'My family and I will never forget Friday, January 20th, 2017. Thank
you!'
string into database than b'My give error so you need do add slashes before ' like
b\'My family and I will never forget Friday, January 20th, 2017. Thank
you!\'
Mysql function mysql_real_escape_string automatically handle this problem so you just change your string using mysql_real_escape_string function
if you use mysqli than it's mysqli_real_escape_string function
Full Example code
$link = mysqli_connect('localhost', '***', '***');
$r = "b'My family and I will never forget Friday, January 20th, 2017. Thank you!'";
//echo $r;
$r = "b'My family and I will never forget Friday, January 20th, 2017. Thank you!'";
echo mysqli_real_escape_string($link,$r);
Sorry for my bad english
I'm using JDBC to add some rows to my DB and they have a date aspect to it.
The GMT String for the date I'm adding in the commit is
11 Jan 2014 05:00:00 GMT
but when I get it back I'm getting this:
11 Jan 2014 04:00:00 GMT
I'm 1 hour ahead of my server and this might be part of the problem. I tried using datetime and timestamp but none of these solved my problem.
my JDBC query looks like this: (it's in Scala)
val statement = connection.prepareStatement("INSERT INTO `listings`(`track_id`, `date`, `position`) VALUES (?,?,?)");
statement.setInt(1, trackId);
statement.setDate(2,trackListing.date);
statement.setInt(3, trackListing.position);
statement.execute();
Is there any way to avoid this problem?
Turns out that by changing setDate and getDate to setTimestamp and getTimestamp I could fix it. I ended up just wrapping the timestamps to dates to maintain compatiblity, using:
new java.sql.Date(rs.getTimestamp("date").getTime());
Hope this helps someone else with the same problem in the future.
Query
Get the Joining year,Joining Month and Joining Date from employee table
This is my query which I have to perform. For this I write the following script:
select
SUBSTRING (convert(varchar,joining_date,103),7,4) ,
SUBSTRING (convert(varchar,joining_date,100),1,3) ,
SUBSTRING (convert(varchar,joining_date,100),5,2)
from
EMPLOYEE
The result is: http://d.pr/i/vObI
But when I changed convert(varchar,joining_date,100) to convert(varchar,joining_date,101)
Result is like this: http://d.pr/i/G5fZ
Can anyone please explain what this parameter means?
There are several different ways that you can format date using convert(varchar.... These are well documented on the MSDN site or different sites online.
Using convert(varchar..., date, 100) places the date in the format:
mon dd yyyy hh:mmAM (or PM)
May 10 2013 12:55PM
Using convert(varchar...date, 101) puts the date in the format:
mm/dd/yyyy
05/10/2013
See Demo
My suggestion would be whenever you implement these conversions, be sure to give a length on the varchar(10), etc.
Based on what it looks like you are returning, you can eliminate some of the convert/substring statements that you are using and implement some other functions to get the same result:
select year(joining_date) as [year] ,
convert(varchar(3),joining_date,100) as [month] ,
day(joining_date) as [day]
from EMPLOYEE
I think there is a better approach to split and get datetime parts using DATEPART:
select DATEPART(YEAR, [joining_date]),
DATEPART(MONTH, [joining_date]),
DATEPART(DAY, [joining_date]) from EMPLOYEE
or if you are interested for example in names use DATENAME:
select DATEPART(YEAR, [joining_date]),
DATENAME(MONTH, [joining_date]),
DATEPART(DAY, [joining_date]) from EMPLOYEE
However according to MSDN for DATENAME: "The return value depends on the language environment set by using SET LANGUAGE".
Regarding your initial question - these parameters are basically styles or I would call them Regional specific codes as described here, and you can just run the different queries against the DB and you will see the strings returned - and you will figure why you get unexpected results. For more infos refer to MSDN: CAST and CONVERT
My server default timezone is +0400 (Moscow). All instance recording to DB in UTC format. IE: it user create item at 04-00 this is recorded as 00-00. When I'm trying to get
Item.last
I see the UTC raw time. When I'm asking
Item.last.created_at
I've get +0400 time. BUT! When I'm using std time functions like
Item.where('created_at > ?', Time.now.beginning_of_day)
it is making sql query
SELECT `items`.* FROM `items` WHERE (items.created_at > '2012-11-30 00:00:00')
instead of
SELECT `items`.* FROM `items` WHERE (items.created_at > '2012-11-29 20:00:00')
So i'm losing 4 hours. The little crunch is using in_time_zone:
Time.now.beginning_of_day.in_time_zone(-4)
But I's not a jedi way :D Any ideas?
I hope your users have some settings related to their time zones in their profiles. In this case you can do the following:
Time.use_zone(zone_of_current_user) do # like 'America/New_York'
#products = Product.where('created_at > ?', Time.zone.now.beginning_of_day)
end
This way you'll set the zone for the block.
Note the using of zone method of Time.
I think i've solved it:
Time.now.beginning_of_day.in_time_zone('UTC')
=> Thu, 29 Nov 2012 20:00:00 UTC +00:00
So this is relation what time in Moscow when day beginnings in UTC timezone :)